如何将python中多层列表转换为单层列表
1、 对于使用python,还可以使用flatten函数来做: >>> d=[1,2,3,[4,5],[6,7,[8,9]]] >>>from compiler.ast import flatten >>>flatten(a) 【1, 2, 3, 4, 5, 6,7,8】

2、对于规范的且嵌套维度较低的多维列表,python中有很多方法可以实现:
a=[[1,2],[3,4],[5,6]]
print [j for i in li for j in i]
#or
from itertools import chain
print list(chain(*a))
#or
import itertools

3、对于规范的且嵌套维度较低的多维列表,python中有很多方法可以实现:
a=[[1,2],[3,4],[5,6]]
print [j for i in li for j in i]
#or
from itertools import chain
print list(chain(*a))
#or
import itertools

4、 #生成式的方法还有很多,可以自行摸索 作为一个函数返回值,将多个数据结果作为list元素返回,返回值就成了一个多层嵌套的list

5、返回值作为修改填充的参数,在嵌套的列表中自然不适合提取遍历自动填充,因此就想到有没有方法将嵌套的list转成单层的列表便于数据提取。在没有现成模块方法的情况下,最简单的方法就是使用一个空列表,遍历嵌套列表,将元素顺次追加到空列表中:

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
阅读量:22
阅读量:40
阅读量:145
阅读量:74
阅读量:79