clementescolano
clementescolano

Reputation: 505

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 !

Upvotes: 3

Views: 1550

Answers (1)

hpaulj
hpaulj

Reputation: 231655

Try

ind=tuple(2,2)
x[ind]

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.

Upvotes: 2

Related Questions