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.py412 chars18 lines
This will output:
main.py90 chars3 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