8

One can use numpy's extract function to match an element in an array. The following code matches an element 'a.' exactly in an array. Suppose I want to match all elements containing '.', how would I do that? Note that in this case, there would be two matches. I'd also like to get the row and column number of the matches. The method doesn't have to use extract; any method will do. Thanks.

In [110]: x = np.array([['a.','cd'],['ef','g.']])

In [111]: 'a.' == x
Out[111]: 
array([[ True, False],
       [False, False]], dtype=bool)

In [112]: np.extract('a.' == x, x)
Out[112]: 
array(['a.'], 
      dtype='|S2')
Faheem Mitha
  • 6,096
  • 7
  • 48
  • 83

2 Answers2

9

You can use the string operations:

>>> import numpy as np
>>> x = np.array([['a.','cd'],['ef','g.']])
>>> x[np.char.find(x, '.') > -1]
array(['a.', 'g.'], 
      dtype='|S2')

EDIT: As per request in the comments... If you want to find out the indexes of where the target condition is true, use numpy.where:

>>> np.where(np.char.find(x, '.') > -1)
(array([0, 1]), array([0, 1]))

or

>>> zip(*np.where(np.char.find(x, '.') > -1))
[(0, 0), (1, 1)]
mac
  • 42,153
  • 26
  • 121
  • 131
  • 3
    Nice, never knew about `char` – Benjamin Dec 06 '11 at 21:39
  • Thanks. Any way of finding the row and column? – Faheem Mitha Dec 06 '11 at 21:49
  • @mac: See comment at the bottom of the main question. I couldn't fit it into SO's notion of a comment. – Faheem Mitha Dec 06 '11 at 22:12
  • @FaheemMitha - Sorry, my bad. You should compare with `>-1` as `0` is the valid positional index for "found as first character"... (You should probably remove the edited part if it solves, as it will only confuse future visitors...). – mac Dec 06 '11 at 22:16
3

How about this?

>>> import numpy as np
>>> x = np.array([['a.','cd'],['ef','g.']])
>>> selector = np.array(['.' in s for s in x.flat]).reshape(x.shape)
>>> x[selector]
array(['a.', 'g.'], 
      dtype='|S2')
jterrace
  • 64,866
  • 22
  • 157
  • 202