You can select elements in numpy arrays as follows
a = np.random.rand(100) sel = a > 0.5
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?
python numpy regex
Boris Gorelik
source share