这是我的代码:
[[[3], [4]], [[5], [6]], [[7], [8]]]
我怎样才能把它改成:
[[3], [4], [5], [6], [7], [8]]
?
您想要平展输入列表的单个级别,请使用列表推导式尝试以下解决方案:
lst = [[[3], [4]], [[5], [6]], [[7], [8]]]
[e for sl in lst for e in sl]
=> [[3], [4], [5], [6], [7], [8]]
试试这个:
import numpy as np
a = np.array([[[3],[4]],[[5],[6]],[[7],[8]]])
b = a.reshape(6,1)
未压缩或很长的路:
l3d = [[[3], [4]], [[5], [6]], [[7], [8]]]
l2d = []
for e1 in l3d:
for e2 in e1:
l2d.append(e2)
# Provided list
lst = [[[3], [4]], [[5], [6]], [[7], [8]]]
# To do this we are going to import chain from itertools
from itertools import chain
final = list(chain(*lst))
print(final) # Output: [[3], [4], [5], [6], [7], [8]]