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

发布于 2024-11-29 01:13:40 字数 450 浏览 0 评论 0原文

我想了解如何操作二维数组的元素。

例如,如果我有:

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

我已经在 python 中定义了它们,例如:

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

我看到我不能引用 a[1][1] 而是引用 a[1]这给出了 [2,1] 的结果。 所以,我不明白如何访问这些数组的第二行?那将是a21、a22、a23、b21、b22、b23? 我该如何将它们相乘为 c1 = a21*b21, c2 = a22*b22 等?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

漫漫岁月 2024-12-06 01:13:40

如果你有

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

那么

a[1][1]

就会工作得很好。正如您想要的那样,它指向第二列、第二行。

我不确定你做错了什么。

要将第三列中的单元格相乘,您只需执行

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

“这适用于任意行数”的操作。

编辑:第一个数字是列,第二个数字是行,以及您当前的布局。它们都从开始编号。如果你想切换顺序,你可以这样做

a = zip(*a)

,或者你可以这样创建它:

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

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]]
寄与心 2024-12-06 01:13:40

如果你想用二维数组做很多计算,你应该使用 NumPy 数组而不是嵌套列表。

对于您的问题,您可以使用: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)

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)
稳稳的幸福 2024-12-06 01:13:40

似乎在这里工作:

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

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
半衾梦 2024-12-06 01:13:40

仔细看看你的数组有多少个括号。我遇到了一个例子,当函数返回带有额外括号的答案时,如下所示:

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

这不起作用

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

这可以打开括号:

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

现在可以使用普通的元素访问表示法:

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

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
原来分手还会想你 2024-12-06 01:13:40

如果您有:

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

您可以使用以下方式轻松访问它:

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

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)
梦一生花开无言 2024-12-06 01:13:40

a[1][1] 确实按预期工作。您的意思是 a11 作为第一行的第一个元素吗?因为那将是[0][0]。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文