将嵌套列表转换为一维列表将嵌套列表转换为一维列表

在处理数据时,有时我们会遇到嵌套列表,即列表中包含其他列表。在某些情况下,我们需要将嵌套列表转换为一维列表,即所有元素都在同一层级。

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]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值