![](https://secure.gravatar.com/avatar/276928028a2075ceeb0b7aface8e2e2a.jpg?s=120&d=mm&r=g)
Hi All, Given an array such as: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) How can I find the index of a particular number in the array? (ie: if it was a list, I'd do [1,2,3,4].index(3)) cheers, Chris
![](https://secure.gravatar.com/avatar/3325e461df2fda8738f35a8bf4fd735e.jpg?s=120&d=mm&r=g)
On Wed, Oct 6, 2010 at 5:26 AM, Chris Withers <chris@simplistix.co.uk>wrote:
Hi All,
Given an array such as:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
How can I find the index of a particular number in the array?
(ie: if it was a list, I'd do [1,2,3,4].index(3))
cheers,
Chris _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Try with numpy.where(array == value) and plain "array == value" IIRC, the first will return an array the size of the found elements and the second will return an array (or list?) of True/False. Either can be used as "index = numpy.where()" and "index = (array == value)" and then "array[index]". N
![](https://secure.gravatar.com/avatar/09939f25b639512a537ce2c90f77f958.jpg?s=120&d=mm&r=g)
On Wed, Oct 6, 2010 at 7:23 AM, Nicolas Bigaouette <nbigaouette@gmail.com>wrote:
On Wed, Oct 6, 2010 at 5:26 AM, Chris Withers <chris@simplistix.co.uk>wrote:
Hi All,
Given an array such as:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
How can I find the index of a particular number in the array?
(ie: if it was a list, I'd do [1,2,3,4].index(3))
cheers,
Chris _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Try with numpy.where(array == value) and plain "array == value" IIRC, the first will return an array the size of the found elements and the second will return an array (or list?) of True/False. Either can be used as "index = numpy.where()" and "index = (array == value)" and then "array[index]". N
Wouldn't it be more technically correct to use numpy.nonzero()? Returning the index is a special behavior of not specifying the second argument to numpy.where(). Ben Root
![](https://secure.gravatar.com/avatar/3513a9ae4099702cd06728b1537ee6f7.jpg?s=120&d=mm&r=g)
Chris, You can use where() command to find index of a particular number in the array. For e.g. to find index of number 1 in array a, you can say idx = where(a==1) Regards, Brian -----Original Message----- From: numpy-discussion-bounces@scipy.org [mailto:numpy-discussion-bounces@scipy.org] On Behalf Of Chris Withers Sent: Wednesday, October 06, 2010 5:27 AM To: numpy-discussion@scipy.org Cc: snezana.pejic@glcuk.com Subject: [Numpy-discussion] index of a value in an array Hi All, Given an array such as: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) How can I find the index of a particular number in the array? (ie: if it was a list, I'd do [1,2,3,4].index(3)) cheers, Chris _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
![](https://secure.gravatar.com/avatar/3a8105539272345aef5c305505f2e2a7.jpg?s=120&d=mm&r=g)
Chris, Note that
where(condition) actually returns a tuple (one item for each dimension of a) and each element is an array with the index of when your condition occurs. Therefore if you want to extract the index itself, you need to write idx = where(a==1)[0][0] for the first element, and where(a==1)[0] for the array of all indices.
Best, Jonathan On Wed, Oct 6, 2010 at 8:53 AM, Thomas, Brian (GE Energy) < brian1.thomas@ge.com> wrote:
Chris,
You can use where() command to find index of a particular number in the array.
For e.g. to find index of number 1 in array a, you can say idx = where(a==1)
Regards, Brian
-----Original Message----- From: numpy-discussion-bounces@scipy.org [mailto:numpy-discussion-bounces@scipy.org] On Behalf Of Chris Withers Sent: Wednesday, October 06, 2010 5:27 AM To: numpy-discussion@scipy.org Cc: snezana.pejic@glcuk.com Subject: [Numpy-discussion] index of a value in an array
Hi All,
Given an array such as:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
How can I find the index of a particular number in the array?
(ie: if it was a list, I'd do [1,2,3,4].index(3))
cheers,
Chris _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
-- Jonathan Rocher, Enthought, Inc. jrocher@enthought.com 1-512-536-1057 http://www.enthought.com
participants (5)
-
Benjamin Root
-
Chris Withers
-
Jonathan Rocher
-
Nicolas Bigaouette
-
Thomas, Brian (GE Energy)