Blog Archive

Friday, August 3, 2012

Searching Numpy Arrays


Suppose you have an array like this....

>>> nums
array([20,  2, 24,  6,  7,  3,  2])

And you want to know where numbers less than 3 are located. In that case simply use,

>>> theindex=np.where(nums<3)
>>> theindex
(array([1, 6]),)

If you only want to know the location of the first number less than 3, simply do this:

>>> theindex=np.where(nums<3)[0][0]
>>> theindex
1

No comments:

Post a Comment