我试图弄清楚为什么以下代码不起作用:
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
有人可以解释一下,为什么第一个列表不能被串联,而其他人可以吗?
相关分类