[Numpy-discussion] Masking an array with another array
Wed Apr 22 20:03:57 CDT 2009
On Wed, Apr 22, 2009 at 8:18 PM, Gökhan SEVER <firstname.lastname@example.org> wrote:
> Yes Pierre,
> I like this one line of elegances in Python a lot. I was thinking that the
> answer lies in somewhere in masked array operations, but I proved wrong.
> Thanks for your input on this small riddle.
> Here is another way of doing that. (That's what I thought of initially and
> what Matthias Michler responded on matplotlib mailing list.)
> mask = zeros(len(a), dtype=bool)
> for index in xrange(len(a)): # run through array a
> if a[index] in b:
> mask[index] = True
> Ending with a quote about Pythonicness :)
> "...that something is Pythonic when it has a sense of quality, simplicity,
> clarity and elegance about it."
> On Wed, Apr 22, 2009 at 4:49 PM, Pierre GM <email@example.com> wrote:
>> On Apr 22, 2009, at 5:21 PM, Gökhan SEVER wrote:
>> > Hello,
>> > Could you please give me some hints about how to mask an array using
>> > another arrays like in the following example.
>> What about that ?
>> numpy.logical_or.reduce([a==i for i in b])
I prefer broad casting to list comprehension in numpy:
>>> a = np.arange(5)
>>> b = np.array([2,3])
array([False, False, True, True, False], dtype=bool)
More information about the Numpy-discussion