例如:
复制代码
1
2
3li=[[1,2],[3,4],[5,6]] print [j for i in li for j in i] #or
复制代码
1
2
3
4
5
6
7
8from itertools import chain print list(chain(*li)) #or a=[[1,2],[3,4],[5,6]] t=[] [t.extend(i) for i in a] print t #or
复制代码
1print sum(li,[])
对于复杂一些的,如:li=[1,[2],[[3]],[[4,[5],6]]],上面的方法就不好使了,得换个方法了,
从结构上看像是树状的,很容易联想到了目录的遍历,于是就有了下面的做法:
复制代码
1
最后
以上就是强健期待最近收集整理的关于python之flatten操作的全部内容,更多相关python之flatten操作内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复