以此为本,以此为本,以此为本,以此为本,以此为本,以此为本,以此为本。
如果您的列表来自列表理解,问题可以通过纠正理解更简单/直接解决;请参见Python列表理解;压缩列表?
在这里,最受欢迎的解决方案通常只包含一个“层”的清单。 查看清单不规则(自愿清单)的清单,解决方案完全清单一个深度清单的结构(重复,一般)。
以此为本,以此为本,以此为本,以此为本,以此为本,以此为本,以此为本。
如果您的列表来自列表理解,问题可以通过纠正理解更简单/直接解决;请参见Python列表理解;压缩列表?
在这里,最受欢迎的解决方案通常只包含一个“层”的清单。 查看清单不规则(自愿清单)的清单,解决方案完全清单一个深度清单的结构(重复,一般)。
当前回答
你可以简单地使用Pandas这样做:
import pandas as pd
pd.Series([[1, 2, 3], [4, 5, 6], [7], [8, 9]]).sum()
其他回答
def flatten_array(arr):
result = []
for item in arr:
if isinstance(item, list):
for num in item:
result.append(num)
else:
result.append(item)
return result
print(flatten_array([1, 2, [3, 4, 5], 6, [7, 8], 9]))
// output: [1, 2, 3, 4, 5, 6, 7, 8, 9]
最简单的方式在Python没有任何图书馆
此功能还将适用于多维列表。
使用 recursion 我们可以实现列表中的任何组合,我们可以无需使用任何图书馆。
#Devil
x = [[1, 2, 3], [4, 5, 6], [7], [8, 9]]
output = []
def flatten(v):
if isinstance(v, int):
output.append(v)
if isinstance(v, list):
for i in range(0, len(v)):
flatten(v[i])
flatten(x)
print("Output:", output)
#Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]
#Adding more dimensions
x = [ [1, [2, 3, [4, 5], [6]], 7 ], [8, [9, [10]]] ]
flatten(x)
print("Output:", output)
#Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
def flatten(alist):
if alist == []:
return []
elif type(alist) is not list:
return [alist]
else:
return flatten(alist[0]) + flatten(alist[1:])
我创建了一点功能,基本上可以平滑任何东西. 你可以用管道:管道安装平滑一切
from flatten_everything import flatten_everything
withoutprotection=list(
flatten_everything(
[
1,
1,
2,
[3, 4, 5, [6, 3, [2, 5, ["sfs", "sdfsfdsf",]]]],
1,
3,
34,
[
55,
{"brand": "Ford", "model": "Mustang", "year": 1964, "yearxx": 2020},
pd.DataFrame({"col1": [1, 2], "col2": [3, 4]}),
{"col1": [1, 2], "col2": [3, 4]},
55,
{"k32", 34},
np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]),
(np.arange(22), np.eye(2, 2), 33),
],
]
)
)
print(withoutprotection)
output:
[1, 1, 2, 3, 4, 5, 6, 3, 2, 5, 'sfs', 'sdfsfdsf', 1, 3, 34, 55, 'Ford', 'Mustang', 1964, 2020, 1, 2, 3, 4, 1, 2, 3, 4, 55, 34, 'k32', 1, 2, 3, 4, 5, 6, 7, 8, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 1.0, 0.0, 0.0, 1.0, 33]
你甚至可以保护物体免受闪烁:
from flatten_everything import ProtectedDict,ProtectedList,ProtectedTuple
withprotection=list(
flatten_everything(
[
1,
1,
2,
[3, 4, 5, [6, 3, [2, 5, ProtectedList(["sfs", "sdfsfdsf",])]]],
1,
3,
34,
[
55,
ProtectedDict({"brand": "Ford", "model": "Mustang", "year": 1964, "yearxx": 2020}),
pd.DataFrame({"col1": [1, 2], "col2": [3, 4]}),
{"col1": [1, 2], "col2": [3, 4]},
55,
{"k32", 34},
np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]),
ProtectedTuple((np.arange(22), np.eye(2, 2), 33)),
],
]
)
)
print(withprotection)
output:
[1, 1, 2, 3, 4, 5, 6, 3, 2, 5, ['sfs', 'sdfsfdsf'], 1, 3, 34, 55, {'brand': 'Ford', 'model': 'Mustang', 'year': 1964, 'yearxx': 2020}, 1, 2, 3, 4, 1, 2, 3, 4, 55, 34, 'k32', 1, 2, 3, 4, 5, 6, 7, 8, (array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,17, 18, 19, 20, 21]), array([[1., 0.], [0., 1.]]), 33)]
matplotlib.cbook.flatten() 将为粘贴列表工作,即使它们比示例更深地粘贴。
import matplotlib
l = [[1, 2, 3], [4, 5, 6], [7], [8, 9]]
print(list(matplotlib.cbook.flatten(l)))
l2 = [[1, 2, 3], [4, 5, 6], [7], [8, [9, 10, [11, 12, [13]]]]]
print(list(matplotlib.cbook.flatten(l2)))
结果:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
这比 underscore 快 18 倍。
Average time over 1000 trials of matplotlib.cbook.flatten: 2.55e-05 sec
Average time over 1000 trials of underscore._.flatten: 4.63e-04 sec
(time for underscore._)/(time for matplotlib.cbook) = 18.1233394636