Python将任意维度的不规则列表展平成一维列表

本文使用Python将任意维度的不规则列表展平成一维列表;

实现代码

example = [1,[[2],[[[3,[4,[5,[[6]],7,[8]]]]]]],[[[[[[9]]],10]]]]

flatten = lambda l: sum(map(flatten,l),[]) if isinstance(l,list) else [l]

print(flatten(example))

需要进行处理的列表:

example = [1,[[2],[[[3,[4,[5,[[6]],7,[8]]]]]]],[[[[[[9]]],10]]]]

输出

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

原创内容,如需转载,请注明出处;

本文地址: https://www.perfcode.com/p/python-flatten-list.html

分类: 计算机技术
推荐阅读:
MIPS处理器的基本特性和体系结构 MIPS处理器的基本特性和体系结构包括以下方面:
pip安装和更新scikit-learn(sklearn) scikit-learn(sklearn)是一个Python第三方提供的一个非常强大的机器学习库;使用pip工具安装方法如下:
PySide6使用Qt Designer创建的UI 在本文中,你将学会使用Qt Designer画出UI,并在Python中使用它;
Python数组求和 给定一个数组,使用Python进行数组求和;
pg_restore.exe file not found 解决方法 使用pgAdmin时出现类似 pg_restore.exe file not found. Please correct the Binary Path in the Prefer 错误:
WordPress如何禁用Emoji表情 由于WordPress自带的Emoji表情服务需要连接到国外服务器,国内用户访问比较慢、不稳定,影响体验,且大部分用户根本用不到;这个服务应该禁用。