Selecting elements in a numpy array using regular expressions

You can select elements in numpy arrays as follows

a = np.random.rand(100) sel = a > 0.5 #select elements that are greater than 0.5 a[sel] = 0 #do something with the selection b = np.array(list('abc abc abc')) b[b==a] = 'A' #convert all the a to A's 

This property is used by the np.where function to retrieve indexes:

 indices = np.where(a>0.9) 

What I would like to do is use regular expressions in such an element selection. For example, if I want to select the elements from b above that correspond to [Aab] regexp, I need to write the following code:

 regexp = '[Ab]' selection = np.array([bool(re.search(regexp, element)) for element in b]) 

It looks too verbouse for me. Is there a shorter and more elegant way to do this?

+8
python numpy regex
source share
1 answer

There is some kind of setup here, but if numpy does not have any direct support for regular expressions, which I donโ€™t know about, then this is the most โ€œno-bitโ€ solution. It tries to make iteration over an array more efficient than standard python iteration.

 import numpy as np import re r = re.compile('[Ab]') vmatch = np.vectorize(lambda x:bool(r.match(x))) A = np.array(list('abc abc abc')) sel = vmatch(A) 
+15
source share

All Articles