[Numpy-discussion] Boolean indexing

Sam Tygier Sam.Tygier at hep.manchester.ac.uk
Tue Mar 8 08:59:32 EST 2011


Hi

I am having an  issue with boolean slicing. it seems to work fine for
reading a value, but I can use it to set a value:

import numpy
b =  numpy.array([[1,2],[3,4],[5,6],[7,8],[9,10]])
m = numpy.array([0,1,0,0,0], dtype=bool)
print b[m]
print b[m][0,0]
b[m][0,0] = -1
print b[m][0,0]

I think the boolean slicing is making a copy instead of a view. is there
a way around this? 

My full problem is to modify a column of a rec array, if another column
has a condition. ie, a neat way of doing

for n in xrange(len(a)):
  if a[n]['name'] == "foo":
    a[n]['x'] += 1

i have been trying things like:
a[a[n]['name'] == "foo"]['x'] += 1
but does not work.

I am using
Python version 2.7 (r27:82500, Sep 16 2010, 18:02:00)
numpy 1:1.4.1-6.fc14 (fedora package)

Thanks

Sam




More information about the NumPy-Discussion mailing list