[SciPy-user] indices of array elements that satisfy some condition.

James Battat jbattat at cfa.harvard.edu
Tue May 2 16:22:22 EDT 2006


Hi all,

I'm coming from an IDL (RSI) background in which you can get the
indices of an array whose elements satisfy some condition in the following
way:

IDL> data = [10, 5, 9, 14]
IDL> ids = where(data>9)
IDL> print, ids
  0, 3
IDL> print, data[ids]
  10, 14

Using scipy, I only know of a very convoluted solution to the same problem
(see below).  Can someone suggest a more direct, cleaner approach?

Many thanks,
James

>>> import scipy
>>> data = scipy.array([10,5,9,14])
>>> data
array([10,5,9,14])
>>> true = scipy.ones(len(data))
>>> true
array([1,1,1,1])
>>> false = scipy.zeros(len(data))
>>> false
array([0,0,0,0])
>>> temp = scipy.where(data>9,true,false)
>>> temp
array([1,0,0,1])
>>> ids = scipy.nonzero(temp)
>>> ids
array([0,3])
>>> data[ids]
array([10,14])




More information about the SciPy-User mailing list