切成2个维数阵列

发布于 2025-01-19 01:32:59 字数 354 浏览 1 评论 0原文

我有2个维数阵列,我想在2维列表中获取每个第一个元素。但是,当我尝试使用切片时,我会有奇怪的行为。

arr = [[10,2],[11,3],[12,4],[13,4],[14,5]]
print(arr[:][1])

输出:

[11, 3]

arr = [[10,2],[11,3],[12,4],[13,4],[14,5]]
print(arr[1][:])

输出:

[11, 3]

为什么这种行为?我知道还有另一种方法。我想要一个解释。

I have 2 dimensional array I want to get every first element in 2 dimensional list. But when I try to using slice I have strange behavior.

arr = [[10,2],[11,3],[12,4],[13,4],[14,5]]
print(arr[:][1])

Output:

[11, 3]

arr = [[10,2],[11,3],[12,4],[13,4],[14,5]]
print(arr[1][:])

Output:

[11, 3]

Why is this behaviour? I know there are another ways for this. I would like an explanation.

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

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

发布评论

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

评论(3

¢蛋碎的人ぎ生 2025-01-26 01:33:00

ARR [:]与对象ARR本身一样好,因为您要求切片包含所有元素。检查此输出输出的内容

arr = [[10,2],[11,3],[12,4],[13,4],[14,5]]
print(arr[:])

是输出整个ARR [[10,2],[11,3],[[11,3],[ 12,4],[13,4],[14,5]]

因此,当您打印arr [:] [1]时,索引1的元素,[11,3]正在打印

第二个arr [1] [:]可以理解的是在第二行元素中使用所有元素的第二行元素调用第二行元素的正确方法

arr[:] is as good as object arr itself, because you are calling for a slice to include all elements.Check what this outputs

arr = [[10,2],[11,3],[12,4],[13,4],[14,5]]
print(arr[:])

It outputs the whole arr [[10, 2], [11, 3], [12, 4], [13, 4], [14, 5]]

so when you print arr[:][1], element of index 1, [11,3] is getting printed

Second one arr[1][:] understandably is the proper way to call the second row element in a 2D array with all the elements within second row element

久隐师 2025-01-26 01:33:00

这样想。您当前有一个1 x 5维度数组。换句话说,您的数组中有五个元素。这就是为什么无论您是做print(arr [:] [1])print(arr [1] [:]),您仍然会得到[11,3]
第一个解决方案是指整个列表,然后在数组中找到第二个元素。后一个解决方案是指数组中的第二个元素,然后搜索第二个元素中的所有元素(如果第二个元素是列表),

则如果您拥有2 x 5 Dimension,它会更容易大批。

arr = [[[10, 2], [11, 3], [12, 4], [13, 4], [14, 5]],
 [[12, 6], [4, 3], [2, 1], [14, 12], [11, 10]]]

在这里,如果您执行print(arr [:] [1]),您将获得

[[12, 6], [4, 3], [2, 1], [14, 12], [11, 10]]

预期的搜索整个数组并返回数组中的第二个元素(第二个嵌套列表)。

如果您执行print(arr [1] [:]),您将获得

[[12, 6], [4, 3], [2, 1], [14, 12], [11, 10]]

相同的情况,因为它再次在数组中寻找第二个元素(第二个嵌套列表)搜索嵌套列表中的所有元素。

现在,回到想要的东西,如果您只想在数组中拥有每个第一个元素,则可以做到这一点。

for i in range(len(arr)):
    print(arr[i][0])
    
[10, 2]
[12, 6]

基于上面的循环,您正在寻找数组中嵌套列表的每一行,并返回嵌套列表中的第一个元素。

Think of it this way. You are currently having a 1 x 5 dimension array. In other words, you have five elements in the array. That is why no matter whether you do print(arr[:][1]) or print(arr[1][:]), you will still get [11, 3].
The first solution refers to the whole list and then find the 2nd element in the array. The latter solution refers to the 2nd element in the array, then search for all elements in the 2nd element(if the 2nd element is a list)

Perhaps it will be easier if you have a 2 x 5 dimension array.

arr = [[[10, 2], [11, 3], [12, 4], [13, 4], [14, 5]],
 [[12, 6], [4, 3], [2, 1], [14, 12], [11, 10]]]

Here, if you do print(arr[:][1]), you will get

[[12, 6], [4, 3], [2, 1], [14, 12], [11, 10]]

which is expected as it searches the whole array and returns the 2nd element (the 2nd nested list) in the array.

If you do print(arr[1][:]), you will get

[[12, 6], [4, 3], [2, 1], [14, 12], [11, 10]]

which will be the same too, because it again looks for the 2nd element(the 2nd nested list) in the array first and searches for all the elements in the nested list.

Now, back to what you want, if you only want to have every first element in the array, you can do this.

for i in range(len(arr)):
    print(arr[i][0])
    
[10, 2]
[12, 6]

Based on the loop above, you are looking for every row of the nested list in the array and return the first element in the nested list.

请止步禁区 2025-01-26 01:33:00

您的问题的解决方案

arr = [[10,2],[11,3],[12,4],[13,4],[14,5]]
print([a[0] for a in arr])

或通过转换为 numpy

arr = [[10,2],[11,3],[12,4],[13,4],[14,5]]
a = np.array(arr)
print(a[:,0])

2D Python 列表不是 numpy 数组,它们无法处理切片。

A solution for your problem

arr = [[10,2],[11,3],[12,4],[13,4],[14,5]]
print([a[0] for a in arr])

or by converting to numpy

arr = [[10,2],[11,3],[12,4],[13,4],[14,5]]
a = np.array(arr)
print(a[:,0])

2D Python list aren't numpy arrays, they can't handle slicing.

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