[SciPy-user] selection from array
Mon Apr 6 05:51:20 CDT 2009
> 2009/4/6 Eugene Semenko <email@example.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?
>>> arr = np.arange(20, 300, 0.5)
>>> cond = (arr > 30.5) & (arr < 125)
>>> ind = cond.nonzero()
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)
More information about the SciPy-user