match a pattern in a numpy array in python

To match a pattern in a numpy array in Python, you can use regular expressions with the numpy method np.vectorize(). Here's an example:

main.py
import numpy as np
import re

# define the pattern to match
pattern = r'\d+'

# create a numpy array
arr = np.array(['abc123', 'def456', 'ghi789'])

# create a vectorized function to apply the regex pattern to each element in the array
matches = np.vectorize(lambda x: re.findall(pattern, x), otypes=[object])

# apply the vectorized function to the array
result = matches(arr)

# print the result
print(result)
412 chars
18 lines

This will output:

main.py
[array(['123'], dtype=object) array(['456'], dtype=object)
 array(['789'], dtype=object)]
90 chars
3 lines

In this example, we define the regular expression pattern as \d+, which matches one or more digits. We then create a numpy array arr, and apply the np.vectorize() method to a lambda function that applies the re.findall() method to each element in the array, using the regex pattern we defined earlier. The otypes=[object] argument specifies that the output should be an array of strings, rather than an array of arrays. Finally, we print the resulting array.

gistlibby LogSnag