Access elements in 2d array python

I would like to understand how one goes about manipulating the elements of a 2D array.

If I have for example:

a= [ a11 a12 a13 ]  and b = [b11 b12 b13] 
     a21 a22 a23             b21 b22 b23

I have defined them in python as for example:

a=[[1,1],[2,1],[3,1]]
b=[[1,2],[2,2],[3,2]]

I saw that I cannot refer to a[1][1] but to a[1] which gives me a result of [2,1]. So, I don't understand how do I access the second row of these arrays? That would be a21, a22, a23, b21, b22, b23? And how would I do in order to multiply them as c1 = a21*b21, c2 = a22*b22, etc ?

Gino Mempin

20.8k24 gold badges84 silver badges111 bronze badges

asked Aug 11, 2011 at 7:20

3

If you have

a=[[1,1],[2,1],[3,1]]
b=[[1,2],[2,2],[3,2]]

Then

a[1][1]

Will work fine. It points to the second column, second row just like you wanted.

I'm not sure what you did wrong.

To multiply the cells in the third column you can just do

c = [a[2][i] * b[2][i] for i in range[len[a[2]]]] 

Which will work for any number of rows.

Edit: The first number is the column, the second number is the row, with your current layout. They are both numbered from zero. If you want to switch the order you can do

a = zip[*a]

or you can create it that way:

a=[[1, 2, 3], [1, 1, 1]]

answered Aug 11, 2011 at 7:27

agfagf

164k40 gold badges278 silver badges232 bronze badges

2

If you want do many calculation with 2d array, you should use NumPy array instead of nest list.

for your question, you can use:zip[*a] to transpose it:

In [55]: a=[[1,1],[2,1],[3,1]]
In [56]: zip[*a]
Out[56]: [[1, 2, 3], [1, 1, 1]]
In [57]: zip[*a][0]
Out[57]: [1, 2, 3]

answered Aug 11, 2011 at 7:29

HYRYHYRY

91.4k25 gold badges183 silver badges186 bronze badges

Look carefully how many brackets does your array have. I met an example when function returned answer with extra bracket, like that:

>>>approx
array[[[[1192,  391]],
       [[1191,  409]],
       [[1209,  438]],
       [[1191,  409]]]]

And this didn't work

>>> approx[1,1]
IndexError: index 1 is out of bounds for axis 1 with size 1

This could open the brackets:

>>> approx[:,0]
array[[[1192,  391],
       [1191,  409],
       [1209,  438],
       [1191,  409]]]

Now it is possible to use an ordinary element access notation:

>>> approx[:,0][1,1]
409

answered Jan 30, 2017 at 11:05

Seems to work here:

>>> a=[[1,1],[2,1],[3,1]]
>>> a
[[1, 1], [2, 1], [3, 1]]
>>> a[1]
[2, 1]
>>> a[1][0]
2
>>> a[1][1]
1

answered Aug 11, 2011 at 7:26

cdhowiecdhowie

149k23 gold badges278 silver badges290 bronze badges

If you have this :

a = [[1, 1], [2, 1],[3, 1]]

You can easily access this by using :

print[a[0][2]]
a[0][1] = 7
print[a]

F Blanchet

1,3003 gold badges18 silver badges31 bronze badges

answered Jun 3, 2019 at 16:26

a[1][1] does work as expected. Do you mean a11 as the first element of the first row? Cause that would be a[0][0].

answered Aug 11, 2011 at 7:28

KenjiKenji

7175 silver badges9 bronze badges

How do you access elements in a 2D array?

An element in 2-dimensional array is accessed by using the subscripts. That is, row index and column index of the array. int x = a[1,1]; Console.

How do you access the elements of a 2D list in Python?

Use list indexing to access elements in a 2D list. Use the list indexing syntax a_2d_list[x][y] to access an element at index y in the nested list at index x .

How do you access the elements of a matrix in Python?

Accessing Values The data elements in a matrix can be accessed by using the indexes. The access method is same as the way data is accessed in Two dimensional array.

How do you iterate a 2D array in Python?

Traversing in a 2D array in python can be done by using a for loop. We can iterate through the outer array first and then at each element of the outer array, we have another array which is our inner array containing the elements. So for each inner array, we run a loop to traverse its elements.

Chủ Đề