Categories
Python Answers

How to find overlapping matches with a regex with Python?

Sometimes, we want to find overlapping matches with a regex with Python.

In this article, we’ll look at how to find overlapping matches with a regex with Python.

How to find overlapping matches with a regex with Python?

To find overlapping matches with a regex with Python, we can use the re.finall method with the r'(?=(\w\w))' regex string.

We have (?=...) to add a lookahead assertion to let us find overlapping matches.

For instance, we write:

import re

matches = re.findall(r'(?=(\w\w))', 'hello')
print(matches)

We call re.findall with the regex string and the string we want to find the matches for.

Therefore, matches is:

['he', 'el', 'll', 'lo']

Conclusion

To find overlapping matches with a regex with Python, we can use the re.finall method with the r'(?=(\w\w))' regex string.

We have (?=...) to add a lookahead assertion to let us find overlapping matches.

Categories
Python Answers

How to convert between datetime, timestamp and datetime64 with Python?

Sometimes, we want to convert between datetime, timestamp and datetime64 with Python.

In this article, we’ll look at how to convert between datetime, timestamp and datetime64 with Python.

How to convert between datetime, timestamp and datetime64 with Python?

To convert between datetime, timestamp and datetime64 with Python, we can use the Pandas’ Timestamp class to create a Pandas timestamp.

And we can use the numpy’s datetime64 method to create a numpy date time object.

For instance, we write:

import numpy as np
import pandas as pd

ts = pd.Timestamp(np.datetime64('2020-05-01T01:00:00.000000'))
print(ts)

dt = np.datetime64('2012-05-01T01:00:00.000000+0100')
print(dt)

We call pd.Timestamp with a nummpy datetime64 object to convert it to a Pandas timestamp.

Then we call the np.datetime64 method with a date time string to convert it to a numpy’s date time 64 object.

Therefore, ts is 2020-05-01 01:00:00.

And dt is 2012-05-01T00:00:00.000000.

Conclusion

To convert between datetime, timestamp and datetime64 with Python, we can use the Pandas’ Timestamp class to create a Pandas timestamp.

And we can use the numpy’s datetime64 method to create a numpy date time object.

Categories
Python Answers

How to split a string and keep the separators with Python?

Sometimes, we want to split a string and keep the separators with Python.

In this article, we’ll look at how to split a string and keep the separators with Python.

How to split a string and keep the separators with Python?

To split a string and keep the separators with Python, we can use the re.split method with the '(\W)' pattern.

For instance, we write:

import re

a = re.split('(\W)', 'foo/bar spam\neggs')
print(a)

We call re.split with '(\W)' and the string we want to split into an array of substrings and assign the array to a.

Therefore, a is ['foo', '/', 'bar', ' ', 'spam', '\n', 'eggs'].

Conclusion

To split a string and keep the separators with Python, we can use the re.split method with the '(\W)' pattern.

Categories
Python Answers

How to count the frequency of the elements in an unordered list with Python?

Sometimes, we want to count the frequency of the elements in an unordered list with Python.

In this article, we’ll look at how to count the frequency of the elements in an unordered list with Python.

How to count the frequency of the elements in an unordered list with Python?

To count the frequency of the elements in an unordered list with Python, we can use the collections.Counter class.

For instance, we write:

import collections

a = [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 5]
counter = collections.Counter(a)

print(counter)

We pass in the a array as the argument for the collections.Counter constructor.

This returns a Counter instance that has the items in a as keys and the count of each item as their values.

Therefore, counter is:

Counter({1: 4, 2: 4, 3: 2, 5: 2, 4: 1})

Conclusion

To count the frequency of the elements in an unordered list with Python, we can use the collections.Counter class.

Categories
Python Answers

How to use the itertools.groupby() method with Python?

Sometimes, we want to use the itertools.groupby() method with Python.

In this article, we’ll look at how to use the itertools.groupby() method with Python.

How to use the itertools.groupby() method with Python?

To use the itertools.groupby() method with Python, we can call it with an array of tuples and a function to do the grouping.

For instance, we write:

from itertools import groupby

things = [("animal", "bear"), ("animal", "duck"), ("plant", "cactus"),
          ("vehicle", "speed boat"), ("vehicle", "school bus")]

groups = [(k, [*g]) for k, g in groupby(things, lambda x: x[0])]
print(groups)

We use list comprehension to get the key and group items from g with the * operator.

We call groupby with things and a function that returns the key of the items to group by, which is the first entry in each tuple.

Therefore, groups is:

[('animal', [('animal', 'bear'), ('animal', 'duck')]), ('plant', [('plant', 'cactus')]), ('vehicle', [('vehicle', 'speed boat'), ('vehicle', 'school bus')])]

Conclusion

To use the itertools.groupby() method with Python, we can call it with an array of tuples and a function to do the grouping.