voidBrain
voidBrain

Reputation: 13

Confused about Python 2D Array indices

I have just started learning Python. Recently, I'm having trouble understanding python 2D arrays! In C, array Dimensions/Subscripts are visual as, int A[5][5] int A[5][5][5].

In Python, with 1 index arr([3,5,7]) is 1D array. But with 3 indices, ([1,2,3],[7,5,4],[6,4,9]) it's considered as 2 array!

Can anybody help me to understand, how many indices can 2D array have in Python and how to recognize dimensions of array?

Upvotes: 1

Views: 166

Answers (2)

BatWannaBe
BatWannaBe

Reputation: 4510

Those aren't indices, you're actually typing in elements. x = np.array([3,5,7]) looks like:

3 5 7

While y = np.array([[1,2,3],[7,5,4],[6,4,9]]) looks like:

1 2 3
7 5 4
6 4 9

Indices are used to access elements/slices of arrays, and an N-dimensional array has N indices. So you can do x[1] to get 5, and y[1,0] to get 7.

Upvotes: 1

U13-Forward
U13-Forward

Reputation: 71610

The part is that the values of the below array are integers:

[3, 5, 7]

It's a single list with numbers in it, so it's only has one dimension.

But the values of the list below are also lists:

[[1, 2, 3], [7, 5, 4], [6, 4, 9]]    

As you can see, it's a nested array, the values aren't integers, the values of the values are integers.

Upvotes: 1

Related Questions