# Untitled

By: a guest on Jun 30th, 2012  |  syntax: None  |  size: 0.89 KB  |  hits: 17  |  expires: Never
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
1. Numpy equivalent of list.index
2. >>> a = np.array([1, 2, 3])
3. >>> np_index(a, 1)
4. 0
5. >>> np_index(a, 10)
6. Traceback (most recent call last):
7.   File "<stdin>", line 1, in <module>
8. ValueError: 10 not in array
9.
10. import numpy as np
11. a = np.array([1, 2, 3])
12. np.min(np.nonzero(a == 2)[0])
13.
14. ValueError: zero-size array to ufunc.reduce without identity
15.
16. >>> from numpy import *
17. >>> y = array([1,3,5,7])
18. >>> indices = (y >= 5).nonzero()
19. >>> y[indices]
20. array([5, 7])
21. >>> nonzero(y)                                # function also exists
22. (array([0, 1, 2, 3]),)
23.
24. import numpy as np
25. a = np.array([10, 20, 30])
26.
27. a.searchsorted(-99) == a.searchsorted(0) == a.searchsorted(10)
28. # returns index 0 for value 10
29.
30. a.searchsorted(20.1) == a.searchsorted(29.9) == a.searchsorted(30)
31. # returns index 2 for value 30
32.
33. a.searchsorted(30.1) == a.searchsorted(99) == a.searchsorted(np.nan)
34. # returns index 3 for undefined value