[英]How can I convert a 3D list into a 2D list in python?
這是我的代碼:
[[[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]]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.