维度均匀
# 使用for降维
n = [[1, 2, 3], [7, 4, 3], [5, 8, 9]]
x = [i for ii in n for i in ii]
print("for降维:", x)
# 使用numpy降维
import numpy as np
n = [[1, 2, 3], [7, 4, 3], [5, 8, 9]]
x = list(np.array(n).ravel())
print("numpy降维:", x)
打印效果
for降维: [1, 2, 3, 7, 4, 3, 5, 8, 9]
numpy降维: [1, 2, 3, 7, 4, 3, 5, 8, 9]
维度无规律
n = [[1, 2, 3], [7, [4], 3], [5, [8, [7]], 9]]
x = list(eval(str(n).replace("[", '').replace("]", ''))) # 维度均匀时也适用
print(x)
打印效果
[1, 2, 3, 7, 4, 3, 5, 8, 7, 9]