以此为本,以此为本,以此为本,以此为本,以此为本,以此为本,以此为本。
如果您的列表来自列表理解,问题可以通过纠正理解更简单/直接解决;请参见Python列表理解;压缩列表?
在这里,最受欢迎的解决方案通常只包含一个“层”的清单。 查看清单不规则(自愿清单)的清单,解决方案完全清单一个深度清单的结构(重复,一般)。
以此为本,以此为本,以此为本,以此为本,以此为本,以此为本,以此为本。
如果您的列表来自列表理解,问题可以通过纠正理解更简单/直接解决;请参见Python列表理解;压缩列表?
在这里,最受欢迎的解决方案通常只包含一个“层”的清单。 查看清单不规则(自愿清单)的清单,解决方案完全清单一个深度清单的结构(重复,一般)。
当前回答
根據您的列表(1, 2, 3), [4, 5, 6], [7], [8, 9] 是 1 列表水平,我們可以簡單地使用數量(列表),而不使用任何圖書館。
sum([[1, 2, 3], [4, 5, 6], [7], [8, 9]],[])
# [1, 2, 3, 4, 5, 6, 7, 8, 9]
延伸此方法的优势,当内部存在一个<unk>或数字时,简单地将每个元素的地图函数添加到列表中
#For only tuple
sum(list(map(list,[[1, 2, 3], (4, 5, 6), (7,), [8, 9]])),[])
# [1, 2, 3, 4, 5, 6, 7, 8, 9]
#In general
def convert(x):
if type(x) is int or type(x) is float:
return [x]
else:
return list(x)
sum(list(map(convert,[[1, 2, 3], (4, 5, 6), 7, [8, 9]])),[])
# [1, 2, 3, 4, 5, 6, 7, 8, 9]
在这里,有一个明确的解释的缺点在记忆的这个方法。 简而言之,它重复创建列表对象,应该避免( )
其他回答
您也可以使用NumPy的公寓:
import numpy as np
list(np.array(l).flat)
它只有在超级列表具有相同的尺寸时才有效。
考虑到列表L的列表,
flat_list = [item for sublist in l for item in sublist]
意思是:
flat_list = []
for sublist in l:
for item in sublist:
flat_list.append(item)
它比迄今为止发布的短篇文章更快(l 是表格的列表)。
下面是相应的功能:
def flatten(l):
return [item for sublist in l for item in sublist]
作为证据,您可以在标准图书馆中使用时间模块:
$ python -mtimeit -s'l=[[1,2,3],[4,5,6], [7], [8,9]]*99' '[item for sublist in l for item in sublist]'
10000 loops, best of 3: 143 usec per loop
$ python -mtimeit -s'l=[[1,2,3],[4,5,6], [7], [8,9]]*99' 'sum(l, [])'
1000 loops, best of 3: 969 usec per loop
$ python -mtimeit -s'l=[[1,2,3],[4,5,6], [7], [8,9]]*99' 'reduce(lambda x,y: x+y,l)'
1000 loops, best of 3: 1.1 msec per loop
解释:基于 + 的缩写(包括在总中使用)是必然的 O(L**2)当有 L 列表时 - 因为中间结果列表保持长,每个步骤都会分配一个新的中间结果列表对象,前中间结果中的所有对象都必须复制(以及在结尾添加一些新的对象)。
列表理解只产生一个列表,一次,并复制每个项目(从其原始居住地到结果列表)也准确一次。
我会建议使用发电机与产量声明和产量从。
from collections.abc import Iterable
def flatten(items, ignore_types=(bytes, str)):
"""
Flatten all of the nested lists to the one. Ignoring flatting of iterable types str and bytes by default.
"""
for x in items:
if isinstance(x, Iterable) and not isinstance(x, ignore_types):
yield from flatten(x)
else:
yield x
values = [7, [4, 3, 5, [7, 3], (3, 4), ('A', {'B', 'C'})]]
for v in flatten(values):
print(v)
这对我来说似乎是最简单的:
>>> import numpy as np
>>> l = [[1, 2, 3], [4, 5, 6], [7], [8, 9]]
>>> print(np.concatenate(l))
[1 2 3 4 5 6 7 8 9]
对于包含多个列表的列表,这里是一个重复的解决方案,为我工作,我希望它是正确的:
# Question 4
def flatten(input_ls=[]) -> []:
res_ls = []
res_ls = flatten_recursive(input_ls, res_ls)
print("Final flatten list solution is: \n", res_ls)
return res_ls
def flatten_recursive(input_ls=[], res_ls=[]) -> []:
tmp_ls = []
for i in input_ls:
if isinstance(i, int):
res_ls.append(i)
else:
tmp_ls = i
tmp_ls.append(flatten_recursive(i, res_ls))
print(res_ls)
return res_ls
flatten([0, 1, [2, 3], 4, [5, 6]]) # test
flatten([0, [[[1]]], [[2, 3], [4, [[5, 6]]]]])
出口:
[0, 1, 2, 3]
[0, 1, 2, 3, 4, 5, 6]
[0, 1, 2, 3, 4, 5, 6]
Final flatten list solution is:
[0, 1, 2, 3, 4, 5, 6]
[0, 1]
[0, 1]
[0, 1]
[0, 1, 2, 3]
[0, 1, 2, 3, 4, 5, 6]
[0, 1, 2, 3, 4, 5, 6]
[0, 1, 2, 3, 4, 5, 6]
[0, 1, 2, 3, 4, 5, 6]
[0, 1, 2, 3, 4, 5, 6]
Final flatten list solution is:
[0, 1, 2, 3, 4, 5, 6]