在处理数据时,有时我们会遇到嵌套列表,即列表中包含其他列表。在某些情况下,我们需要将嵌套列表转换为一维列表,即所有元素都在同一层级。
2、解决方案
将嵌套列表转换为一维列表有几种方法:
- 使用列表推导式:
使用列表推导式可以很容易地将嵌套列表转换为一维列表。语法如下:
newlist = [item for items in newlist for item in items]
其中,newlist 是要转换为一维列表的嵌套列表,item 是嵌套列表中的每个元素。
- 使用 chain from itertools:
itertools 模块提供了 chain 函数,可以将多个可迭代对象连接成一个单一的可迭代对象。我们可以使用 chain 函数将嵌套列表中的所有子列表连接成一个一维列表。语法如下:
from itertools import chain
newlist = list(chain(*newlist))
- 使用 chain.from_iterable:
chain.from_iterable 函数与 chain 函数类似,但它不需要将嵌套列表中的子列表显式地解包。语法如下:
from itertools import chain
newlist = list(chain.from_iterable(newlist))
- 使用 sum 函数:
sum 函数可以将多个列表合并成一个单一的列表。我们可以使用 sum 函数将嵌套列表中的所有子列表合并成一个一维列表。语法如下:
newlist = sum(newlist, [])
- 使用 reduce 函数:
reduce 函数可以将一个列表中的所有元素逐个累加,得到一个最终的结果。我们可以使用 reduce 函数将嵌套列表中的所有子列表累加成一个一维列表。语法如下:
from functools import reduce
newlist = reduce(lambda x,y: x+y, newlist)
代码例子:
newlist =[[52, None, None], [129, None, None], [56, None, None], [111, None, None],
[22, None, None], [33, None, None], [28, None, None], [52, None, None],
[52, None, None], [52, None, None], [129, None, None], [56, None, None],
[111, None, None], [22, None, None], [33, None, None], [28, None, None]]
print("原始列表:")
print(newlist)
# 使用列表推导式
newlist = [item for items in newlist for item in items]
print("\n使用列表推导式将嵌套列表转换为一维列表:")
print(newlist)
# 使用 chain from itertools
from itertools import chain
newlist = list(chain(*newlist))
print("\n使用 chain from itertools 将嵌套列表转换为一维列表:")
print(newlist)
# 使用 chain.from_iterable
from itertools import chain
newlist = list(chain.from_iterable(newlist))
print("\n使用 chain.from_iterable 将嵌套列表转换为一维列表:")
print(newlist)
# 使用 sum 函数
newlist = sum(newlist, [])
print("\n使用 sum 函数将嵌套列表转换为一维列表:")
print(newlist)
# 使用 reduce 函数
from functools import reduce
newlist = reduce(lambda x,y: x+y, newlist)
print("\n使用 reduce 函数将嵌套列表转换为一维列表:")
print(newlist)
输出结果:
原始列表:
[[52, None, None], [129, None, None], [56, None, None], [111, None, None],
[22, None, None], [33, None, None], [28, None, None], [52, None, None],
[52, None, None], [52, None, None], [129, None, None], [56, None, None],
[111, None, None], [22, None, None], [33, None, None], [28, None, None]]
使用列表推导式将嵌套列表转换为一维列表:
[52, None, None, 129, None, None, 56, None, None, 111, None, None, 22,
None, None, 33, None, None, 28, None, None, 52, None, None, 52, None,
None, 52, None, None, 129, None, None, 56, None, None, 111, None,
None, 22, None, None, 33, None, None, 28, None, None]
使用 chain from itertools 将嵌套列表转换为一维列表:
[52, None, None, 129, None, None, 56, None, None, 111, None, None, 22,
None, None, 33, None, None, 28, None, None, 52, None, None, 52, None,
None, 52, None, None, 129, None, None, 56, None, None, 111, None,
None, 22, None, None, 33, None, None, 28, None, None]
使用 chain.from_iterable 将嵌套列表转换为一维列表:
[52, None, None, 129, None, None, 56, None, None, 111, None, None, 22,
None, None, 33, None, None, 28, None, None, 52, None, None, 52, None,
None, 52, None, None, 129, None, None, 56, None, None, 111, None,
None, 22, None, None, 33, None, None, 28, None, None]
使用 sum 函数将嵌套列表转换为一维列表:
[52, None, None, 129, None, None, 56, None, None, 111, None, None, 22,
None, None, 33, None, None, 28, None, None, 52, None, None, 52, None,
None, 52, None, None, 129, None, None, 56, None, None, 111, None,
None, 22, None, None, 33, None, None, 28, None, None]
使用 reduce 函数将嵌套列表转换为一维列表:
[52, None, None, 129, None, None, 56, None, None, 111, None, None, 22,
None, None, 33, None, None, 28, None, None, 52, None, None, 52, None,
None, 52, None, None, 129, None, None, 56, None, None, 111, None,
None, 22, None, None, 33, None, None, 28, None, None]