[Numpy-discussion] where() on mac/pc return different things

James Battat jbattat at cfa.harvard.edu
Wed Jan 23 18:32:06 EST 2008


Hi,

numpy.where() returns different things on my windowsXP machine than on my
collaborator's mac osx machine.

For example, consider:
  >>> import numpy
  >>> a = numpy.array([1,2,3,4])
  >>> b = numpy.where( a > 2 )

On WindowsXP (python 2.4.2, numpy 1.0.1), I get:
  >>> print b
  (array([2, 3]), )
  >>> print b[0]
  [2 3]

While in OSX (python 2.4.3, numpy 0.9.6), he gets:
  >>> print b
  [2, 3]
  >>> print b[0]
  2

This matters because we use where() to get indices to loop over.  On the
mac you can then loop through the b array:

for item in b:
   if b > 5:
      print 'hi'

But this approach fails on a pc because:
   if b > 5:
  >>> ValueError: The truth value of an array with more than one element
  >>> is ambiguous. Use a.any() or a.all()


I'd like to avoid having to do:
  if mac:
    b = numpy.where( a > 5)
  if pc:
    b = numpy.where( a > 5)[0]

Has anybody else notice dealt with this?  Is this a mac/pc difference or a
numpy 1.01 vs. numpy 0.9.6 difference?

Thanks for the help,
James




More information about the NumPy-Discussion mailing list