[Numpy-discussion] Boolean arrays

Friedrich Romstedt friedrichromstedt at gmail.com
Tue Aug 31 14:12:59 EDT 2010


2010/8/27 Brett Olsen <brett.olsen at gmail.com>:
> If there's multiple possible valid values, I've come up with a couple
> possible methods, but they all seem to be inefficient or kludges:
>>>> valid = N.array(("a", "c"))
>>>> (ar == valid[0]) | (ar == valid[1])
> array([ True, False,  True, False, False,  True, False,  True,  True],
> dtype=bool)
>>>> N.array(map(lambda x: x in valid, ar))
> array([ True, False,  True, False, False,  True, False,  True,  True],
> dtype=bool)

(ar[..., numpy.newaxis] == valid).T.sum(axis=0).T > 0

should also do the job.  But it eats up memory.  (It employs broadcasting.)

Friedrich



More information about the NumPy-Discussion mailing list