Python中将嵌套列表扁平化的方法

Python中将嵌套列表扁平化的方法

技术背景

在Python编程中,我们常常会遇到需要将嵌套列表(即列表中包含列表)转换为一个一维的扁平列表的需求。例如,有一个嵌套列表[[1, 2, 3], [4, 5, 6], [7], [8, 9]],我们希望将其转换为[1, 2, 3, 4, 5, 6, 7, 8, 9]。以下将介绍多种实现这一目标的方法。

实现步骤

1. 使用嵌套列表推导式

嵌套列表推导式是一种简洁的实现方式。其基本思路是通过两层循环,将嵌套列表中的每个元素提取出来,组成一个新的扁平列表。

1
2
3
xss = [[1, 2, 3], [4, 5, 6], [7], [8, 9]]
flat_list = [x for xs in xss for x in xs]
print(flat_list)

2. 使用itertools.chain()itertools.chain.from_iterable()

itertools模块提供了高效的迭代工具。chain()函数可以将多个可迭代对象连接起来,而chain.from_iterable()可以直接接受一个可迭代对象作为参数,将其内部的可迭代对象连接起来。

1
2
3
4
5
6
7
8
import itertools
list2d = [[1, 2, 3], [4, 5, 6], [7], [8, 9]]
# 使用 chain()
merged1 = list(itertools.chain(*list2d))
# 使用 chain.from_iterable()
merged2 = list(itertools.chain.from_iterable(list2d))
print(merged1)
print(merged2)

3. 使用sum()函数

sum()函数可以对可迭代对象求和,当对嵌套列表使用时,结合初始值[],可以实现列表的扁平化。但这种方法效率较低,不适合处理大规模数据。

1
2
3
xss = [[1, 2, 3], [4, 5, 6], [7], [8, 9]]
flat_list = sum(xss, [])
print(flat_list)

4. 使用functools.reduce()

reduce()函数可以对序列中的元素进行累积操作。结合operator.concatoperator.iconcat可以实现列表的扁平化。

1
2
3
4
5
6
7
8
9
from functools import reduce
import operator
xss = [[1, 2, 3], [4, 5, 6], [7], [8, 9]]
# 使用 operator.concat
out1 = reduce(operator.concat, xss)
# 使用 operator.iconcat
out2 = reduce(operator.iconcat, xss, [])
print(out1)
print(out2)

5. 自定义递归函数

通过递归的方式,可以处理任意深度的嵌套列表。

1
2
3
4
5
6
7
8
9
10
11
12
from typing import Iterable

def flatten(items):
for x in items:
if isinstance(x, Iterable) and not isinstance(x, (str, bytes)):
yield from flatten(x)
else:
yield x

simple = [[1, 2, 3], [4, 5, 6], [7], [8, 9]]
flat_list = list(flatten(simple))
print(flat_list)

核心代码

以下是上述各种方法的核心代码总结:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import itertools
from functools import reduce
import operator
from typing import Iterable

# 嵌套列表推导式
def nested_list_comprehension(xss):
return [x for xs in xss for x in xs]

# itertools.chain.from_iterable()
def itertools_chain(xss):
return list(itertools.chain.from_iterable(xss))

# sum()
def pythons_sum(xss):
return sum(xss, [])

# functools.reduce() with operator.concat
def reduce_concat(xss):
return reduce(operator.concat, xss)

# functools.reduce() with operator.iconcat
def reduce_iconcat(xss):
return reduce(operator.iconcat, xss, [])

# 自定义递归函数
def custom_flatten(items):
for x in items:
if isinstance(x, Iterable) and not isinstance(x, (str, bytes)):
yield from custom_flatten(x)
else:
yield x

xss = [[1, 2, 3], [4, 5, 6], [7], [8, 9]]
print(nested_list_comprehension(xss))
print(itertools_chain(xss))
print(pythons_sum(xss))
print(reduce_concat(xss))
print(reduce_iconcat(xss))
print(list(custom_flatten(xss)))

最佳实践

  • 小规模数据:对于小规模的嵌套列表,嵌套列表推导式是一种简洁且直观的选择,代码易于理解和维护。
  • 大规模数据:当处理大规模的嵌套列表时,itertools.chain.from_iterable()方法通常具有较高的性能,因为它避免了创建大量的中间列表。
  • 任意深度嵌套:如果嵌套列表的深度不确定,使用自定义的递归函数可以处理任意深度的嵌套结构。

常见问题

1. 性能问题

使用sum()函数和reduce()函数结合operator.concat时,由于每次操作都会创建一个新的列表对象,会导致性能下降,尤其是处理大规模数据时。建议使用itertools.chain.from_iterable()或自定义递归函数。

2. 字符串处理问题

在处理包含字符串的嵌套列表时,需要注意字符串也是可迭代对象。在自定义递归函数中,通常需要排除字符串类型,以避免将字符串拆分为单个字符。例如:

1
2
3
4
5
6
7
8
9
10
11
12
from typing import Iterable

def flatten(items):
for x in items:
if isinstance(x, Iterable) and not isinstance(x, (str, bytes)):
yield from flatten(x)
else:
yield x

complicated = [[1, [2]], (3, 4, {5, 6}, 7), 8, "9"]
flat_list = list(flatten(complicated))
print(flat_list)

3. 空列表处理

在使用某些方法时,如reduce()函数,如果输入的嵌套列表中包含空列表,可能会导致结果不符合预期。在实际使用中,需要根据具体情况进行处理。


Python中将嵌套列表扁平化的方法
https://119291.xyz/posts/2025-04-17.python-flatten-nested-lists/
作者
ww
发布于
2025年4月17日
许可协议