2

So I have this list of Numpy arrays:

import numpy as np
from numpy import array    

m = [array([0,  64]), array([ 0,  79]), array([0,  165]), array([0,  50])]

How do I index the number 50 from the m[3] element in the array?

2
  • @ojy Wow I didn't think that would work. That was too simple it slipped my mind. Thanks! Commented Aug 4, 2014 at 23:55
  • It would probably make more sense for these to be a 2D array, rather than a list of 1D arrays. You might want to have a read of this. Commented Aug 4, 2014 at 23:56

2 Answers 2

5

As already mentioned in the other comments, if your intention is to use a 2D-array, you should create it as:

m = array([[0, 64], [0, 79], [0, 165], [0, 50]])

and then access the elements like:

print(m[3, 1])
Sign up to request clarification or add additional context in comments.

Comments

2

Turns out it is actually accessed like normal python lists:

 m[3][1]

1 Comment

For a proper multidimensional array (rather than just a list of 1D arrays as in your example), this sort of 'chained' indexing will still work, but it's generally faster and easier to use a tuple of indices inside the square brackets. See the docs here for more info on indexing multidimensional arrays.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.