简体   繁体   English

如何访问二维数组的元素?

[英]How to access the elements of a 2D array?

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:我在 python 中定义了它们,例如:

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] .我看到我不能引用a[1][1]而是引用 a a[1]这给了我[2,1]的结果。 So, I don't understand how do I access the second row of these arrays?所以,我不明白如何访问这些 arrays 的第二行? That would be a21, a22, a23, b21, b22, b23 ?那将是a21, a22, a23, b21, b22, b23吗? And how would I do in order to multiply them as c1 = a21*b21, c2 = a22*b22 , etc?我该怎么做才能将它们乘以c1 = a21*b21, c2 = a22*b22等?

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]]

If you want do many calculation with 2d array, you should use NumPy array instead of nest list.如果你想用二维数组做很多计算,你应该使用 NumPy 数组而不是嵌套列表。

for your question, you can use:zip(*a) to transpose it:对于您的问题,您可以使用:zip(*a) 转置它:

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)

Look carefully how many brackets does your array have.仔细查看您的阵列有多少个括号。 I met an example when function returned answer with extra bracket, like that:当 function 返回带有额外括号的答案时,我遇到了一个示例,如下所示:

>>>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

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)

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

a[1][1] does work as expected. a[1][1]确实按预期工作。 Do you mean a11 as the first element of the first row?你的意思是 a11 作为第一行的第一个元素吗? Cause that would be a[0][0].因为那将是[0] [0]。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM