[SciPy-user] selection from array

Scott Sinclair scott.sinclair.za@gmail....
Mon Apr 6 05:51:20 CDT 2009


> 2009/4/6 Eugene Semenko <esemenko@gmail.com>:
>     Hello! I'm new in Python and just started to learn it. My question is
> about method to make selection from array. Suppose there is an array:
> arr = np.arange(20,300,0.5)
> How can I select from this array elements where 30.5 < arr < 125?

Hi Eugene,

>>> arr = np.arange(20, 300, 0.5)
>>> cond = (arr > 30.5) & (arr < 125)
>>> ind = cond.nonzero()
>>> arr[ind]

This works because the False values in the boolean array cond evaluate
to zero, therefore the nonzero method returns the indices where your
condition evaluates to True.

You could also use the special case of np.where() to get the indices
of the entries where your condition is True.

>>> ind = np.where(cond)
>>> arr[ind]

Cheers,
Scott


More information about the SciPy-user mailing list