python 多层list转为一层,python多层list转为,在c,c++等语言中可以
python 多层list转为一层,python多层list转为,在c,c++等语言中可以
在c,c++等语言中可以通过多层嵌套的list来讲多维list转换为二维list
for inner_list in outer_list: for item in inner_list: ...
而在python中我们可以使用list强大的推导式来完成相同的工作:
>>> ll = [[1,2,3],[2,3,4,5]]>>> l = [i for inner in ll for i in inner]>>> l[1, 2, 3, 2, 3, 4, 5]
如果你觉得推导不好理解,也可以用itertools来实现,如下代码:
>>> list_of_menuitems = [['image00', 'image01'], ['image10'], []]>>> import itertools>>> chain = itertools.chain(*list_of_menuitems)
相关内容
- python处理http post payload request类型请求,pythonpayload,普通
- python执行子进程, 设置命令输入,python命令,使用subp
- python minidom toxml方法,minidomtoxml,from xml.dom
- Mixed String type: normal string and unicode,mixedunicode,print 'ni
- Demonstrates the differences between the methods for importing module
- Built-in Numeric Tools,built-innumeric,import math
- Draw shape border,drawshapeborder,from Tkinter
- List indices start at 0, and lists can be sliced, concatenated:,,a
- If statement: a dictionary-based 'switch',,choice =
- Iterating Over Lines with readlines,iteratingreadlines,f = open('fi
评论关闭