以此为本,以此为本,以此为本,以此为本,以此为本,以此为本,以此为本。
如果您的列表来自列表理解,问题可以通过纠正理解更简单/直接解决;请参见Python列表理解;压缩列表?
在这里,最受欢迎的解决方案通常只包含一个“层”的清单。 查看清单不规则(自愿清单)的清单,解决方案完全清单一个深度清单的结构(重复,一般)。
以此为本,以此为本,以此为本,以此为本,以此为本,以此为本,以此为本。
如果您的列表来自列表理解,问题可以通过纠正理解更简单/直接解决;请参见Python列表理解;压缩列表?
在这里,最受欢迎的解决方案通常只包含一个“层”的清单。 查看清单不规则(自愿清单)的清单,解决方案完全清单一个深度清单的结构(重复,一般)。
当前回答
如果我想添加一些东西到以前的答案,这里是我的重复滑板功能,可以滑板不只是滑板列表,但也任何提供的容器或一般任何物品,可以扔出物品。
def flatten(iterable):
# These types won't considered a sequence or generally a container
exclude = str, bytes
for i in iterable:
try:
if isinstance(i, exclude):
raise TypeError
iter(i)
except TypeError:
yield i
else:
yield from flatten(i)
这样,你可以排除你不想要的类型,如 str 或其他。
想法是,如果一个对象可以通过 iter(),它已经准备好产生物品,所以 iterable 甚至可以作为一个对象具有发明器表达式。
有人可以争论:为什么你写了这么一般的,当OP没有要求它?OK,你是对的,我只是觉得这可能帮助某人(就像它为我做的那样)。
测试案例:
lst1 = [1, {3}, (1, 6), [[3, 8]], [[[5]]], 9, ((((2,),),),)]
lst2 = ['3', B'A', [[[(i ** 2 for i in range(3))]]], range(3)]
print(list(flatten(lst1)))
print(list(flatten(lst2)))
出口:
[1, 3, 1, 6, 3, 8, 5, 9, 2]
['3', b'A', 0, 1, 4, 0, 1, 2]
其他回答
我想要一個解決方案,可以處理多種<unk>(<unk>,<unk>,<unk>,<unk>),<unk>,<unk>,<unk>,<unk>,<unk>,<unk>,<unk>,<unk>,<unk>,<unk>,<unk>,<unk>,<unk>。
这就是我所带来的:
def _flatten(l) -> Iterator[Any]:
stack = l.copy()
while stack:
item = stack.pop()
if isinstance(item, list):
stack.extend(item)
else:
yield item
def flatten(l) -> Iterator[Any]:
return reversed(list(_flatten(l)))
和测试:
@pytest.mark.parametrize('input_list, expected_output', [
([1, 2, 3], [1, 2, 3]),
([[1], 2, 3], [1, 2, 3]),
([[1], [2], 3], [1, 2, 3]),
([[1], [2], [3]], [1, 2, 3]),
([[1], [[2]], [3]], [1, 2, 3]),
([[1], [[[2]], [3]]], [1, 2, 3]),
])
def test_flatten(input_list, expected_output):
assert list(flatten(input_list)) == expected_output
要插入深厚的数据结构,请使用 iteration_utilities.deepflatten1:
>>> from iteration_utilities import deepflatten
>>> l = [[1, 2, 3], [4, 5, 6], [7], [8, 9]]
>>> list(deepflatten(l, depth=1))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> l = [[1, 2, 3], [4, [5, 6]], 7, [8, 9]]
>>> list(deepflatten(l))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
这是一个发电机,所以你需要将结果投到列表中,或者明确地对其进行调解。
要单一的平面,如果每一个项目本身是不可分割的,你也可以使用 iteration_utilities.flatten 它本身只是一个薄的旋转器周围 itertools.chain.from_iterable:
>>> from iteration_utilities import flatten
>>> l = [[1, 2, 3], [4, 5, 6], [7], [8, 9]]
>>> list(flatten(l))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
只需添加一些时间表(基于Nico Schlömer的答案,其中不包含此答案中的功能):
此分類上一篇
结果表明,如果 iterable 只包含几个内部 iterables 那么 总数将是最快的,但是,对于长期 iterables 只有 itertools.chain.from_iterable, iteration_utilities.deepflatten 或 nested 理解具有合理的性能, itertools.chain.from_iterable 是最快的(如 Nico Schlömer 已经注意到)。
from itertools import chain
from functools import reduce
from collections import Iterable # or from collections.abc import Iterable
import operator
from iteration_utilities import deepflatten
def nested_list_comprehension(lsts):
return [item for sublist in lsts for item in sublist]
def itertools_chain_from_iterable(lsts):
return list(chain.from_iterable(lsts))
def pythons_sum(lsts):
return sum(lsts, [])
def reduce_add(lsts):
return reduce(lambda x, y: x + y, lsts)
def pylangs_flatten(lsts):
return list(flatten(lsts))
def flatten(items):
"""Yield items from any nested iterable; see REF."""
for x in items:
if isinstance(x, Iterable) and not isinstance(x, (str, bytes)):
yield from flatten(x)
else:
yield x
def reduce_concat(lsts):
return reduce(operator.concat, lsts)
def iteration_utilities_deepflatten(lsts):
return list(deepflatten(lsts, depth=1))
from simple_benchmark import benchmark
b = benchmark(
[nested_list_comprehension, itertools_chain_from_iterable, pythons_sum, reduce_add,
pylangs_flatten, reduce_concat, iteration_utilities_deepflatten],
arguments={2**i: [[0]*5]*(2**i) for i in range(1, 13)},
argument_name='number of inner lists'
)
b.plot()
1 Disclaimer:我是该图书馆的作者
考虑到列表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 列表时 - 因为中间结果列表保持长,每个步骤都会分配一个新的中间结果列表对象,前中间结果中的所有对象都必须复制(以及在结尾添加一些新的对象)。
列表理解只产生一个列表,一次,并复制每个项目(从其原始居住地到结果列表)也准确一次。
你可以使用列表扩展方法. 它显示是最快的:
flat_list = []
for sublist in l:
flat_list.extend(sublist)
表演:
import functools
import itertools
import numpy
import operator
import perfplot
def functools_reduce_iconcat(a):
return functools.reduce(operator.iconcat, a, [])
def itertools_chain(a):
return list(itertools.chain.from_iterable(a))
def numpy_flat(a):
return list(numpy.array(a).flat)
def extend(a):
n = []
list(map(n.extend, a))
return n
perfplot.show(
setup = lambda n: [list(range(10))] * n,
kernels = [
functools_reduce_iconcat, extend, itertools_chain, numpy_flat
],
n_range = [2**k for k in range(16)],
xlabel = 'num lists',
)
出口:
此分類上一篇
這裡有一個通用方法,適用於數字、線條、粘列表和混合容器,這可以讓簡單和複雜的容器混合起來(請參閱Demo)。
代码
from typing import Iterable
#from collections import Iterable # < py38
def flatten(items):
"""Yield items from any nested iterable; see Reference."""
for x in items:
if isinstance(x, Iterable) and not isinstance(x, (str, bytes)):
for sub_x in flatten(x):
yield sub_x
else:
yield x
笔记:
在 Python 3 中,从 flatten(x) 获取可以取代 sub_x 在 flatten(x): 获取 sub_x 在 Python 3.8 中,从 collection.abc 转移到输入模块。
演示
simple = [[1, 2, 3], [4, 5, 6], [7], [8, 9]]
list(flatten(simple))
# [1, 2, 3, 4, 5, 6, 7, 8, 9]
complicated = [[1, [2]], (3, 4, {5, 6}, 7), 8, "9"] # numbers, strs, nested & mixed
list(flatten(complicated))
# [1, 2, 3, 4, 5, 6, 7, 8, '9']
参考
此解決方案是從 Beazley, D. 和 B. Jones. Recipe 4.14, Python Cookbook 3rd Ed., O'Reilly Media Inc. Sebastopol, CA: 2013 發現以前的 SO 帖子,可能是原來的展示。