gpt4 book ai didi

python - numpy.concatenate 如何处理列表

转载 作者:太空宇宙 更新时间:2023-11-04 00:15:08 25 4
gpt4 key购买 nike

我想弄明白,为什么下面的代码不起作用:

import numpy as np

failList = [[[1], [2]],
[[3, 4, 5, 6], [7]],
[[8], [9]],
[[10], [11, 12]],
[[13], [14, 15, 16]]]

goodList = [[[1], [2], [3, 4, 5, 6], [7], [8]],
[[9], [10], [11, 12], [13], [14, 15, 16]]]

goodList2 = [[[1], [2], [3, 4, 5, 6], [7], [8]],
[[9], [10], [11, 12], [13], [14, 15, 16]],
[[9], [10], [11, 12], [13], [14, 15, 16]]]

myLists = [failList, goodList, goodList]

for l in myLists:
print([len(l[i]) for i in range(len(l))])
print([len(l[i][j]) for i in range(len(l)) for j in range(len(l[i]))])
try:
np.concatenate(l)
print("worked")
except:
print("failed")

输出是:

[2, 2, 2, 2, 2]
[1, 1, 4, 1, 1, 1, 1, 2, 1, 3]
failed
[5, 5]
[1, 1, 4, 1, 1, 1, 1, 2, 1, 3]
worked
[5, 5, 5]
[1, 1, 4, 1, 1, 1, 1, 2, 1, 3, 1, 1, 2, 1, 3]
worked

谁能解释一下,为什么第一个列表不能连接而其他列表可以?

最佳答案

concatenate 从每个列表元素创建一个数组,然后将它们连接到所需的轴上。如果形状不匹配,则会引发错误:

In [80]: failList = [[[1], [2]],
...: [[3, 4, 5, 6], [7]],
...: [[8], [9]],
...: [[10], [11, 12]],
...: [[13], [14, 15, 16]]]
...:
In [81]: [np.array(a) for a in failList]
Out[81]:
[array([[1],
[2]]),
array([list([3, 4, 5, 6]), list([7])], dtype=object),
array([[8],
[9]]),
array([list([10]), list([11, 12])], dtype=object),
array([list([13]), list([14, 15, 16])], dtype=object)]
In [82]: [np.array(a).shape for a in failList]
Out[82]: [(2, 1), (2,), (2, 1), (2,), (2,)]
In [83]: np.concatenate([np.array(a) for a in failList])
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-83-c3434632bd7e> in <module>()
----> 1 np.concatenate([np.array(a) for a in failList])

ValueError: all the input arrays must have same number of dimensions

failedList 的元素为不同种类的数组,有的是二维数值,有的是一维对象。 concatenate 不能加入那些。

column_stack 确实有效:

In [87]: np.column_stack(failList)
Out[87]:
array([[1, list([3, 4, 5, 6]), 8, list([10]), list([13])],
[2, list([7]), 9, list([11, 12]), list([14, 15, 16])]],
dtype=object)
In [88]: _.shape
Out[88]: (2, 5)

那是因为它将 (2,) 形状的数组 reshape 为 (2,1)。现在它有一个包含 5 (2,1) 个数组的列表,它可以在第二个维度上连接这些数组,生成一个 (2,5) 数组。但请注意,它是对象数据类型。有些元素是整数,有些是列表(不同大小)。

关于python - numpy.concatenate 如何处理列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51246704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com