Home Ask Login Register

Developers Planet

Your answer is one click away!

clems4ever February 2016

Get a value in a numpy array from a index in a variable

I am trying to access a value in a multi dimensional numpy array. This can be easily done when you know everything, for exemple :

T = numpy.arrange(9).reshape(3, 3) T[2, 2]

And it returns 8, which is what I want. Now, Let's assume [2, 2] is stored in index variable. How can I do to take the value in T with the index stored in index ? I would like to do T[index] but it returns the last row twice (pretty logical but not what I want).

Thank you !


hpaulj February 2016



x[2,2] is the same as x[(2,2)] which is translated into a method call: x.__getitem__((2,2)).

Some numpy functions build an index as a list or array, then convert it to a tuple for use in the index.

Post Status

Asked in February 2016
Viewed 1,165 times
Voted 12
Answered 1 times


Leave an answer

Quote of the day: live life