Python-迭代列表元组列表的方法
列表是一个重要的容器,几乎在日常编程和Web开发的每个代码中都使用列表,使用的次数更多,掌握它的需求也更多,因此必须了解其操作。
示例
# using itertools.ziplongest
# import library
from itertools import zip_longest
# initialising listoflist
test_list = [
[('11'), ('12'), ('13')],
[('21'), ('22'), ('23')],
[('31'), ('32'), ('33')]
]
# printing intial list
print ("Initial List = ", test_list)
# iterate list tuples list of list into single list
res_list = [item for my_list in zip_longest(*test_list)
for item in my_list if item]
# print final List
print ("Resultant List = ", res_list)
# using itertools.ziplongest + lambda + chain
# import library
from itertools import zip_longest, chain
# initialising listoflist
test_list = [
[('11'), ('12'), ('13')],
[('21'), ('22'), ('23')],
[('31'), ('32'), ('33')]
]
# printing intial list
print ("Initial List = ", test_list)
# iterate list tuples list of list into single list
# using lambda + chain + filter
res_list = list(filter(lambda x: x, chain(*zip_longest(*test_list))))
# print final List
print ("Resultant List = ", res_list)
# list using list comprehension
# initialising listoflist
test_list = [
[('11'), ('12'), ('13')],
[('21'), ('22'), ('23')],
[('31'), ('32'), ('33')]
]
# printing intial list
print ("Initial List = ", test_list)
# iterate list tuples list of list into single list
# using list comprehension
res_list = [item for list2 in test_list for item in list2]
# print final List
print ("Resultant List = ", res_list)热门推荐
10 祝女儿简短祝福语大全
11 大学新年祝福语简短创意
12 元旦适合的祝福语简短
13 朋友出远门祝福语简短
14 初六简短的祝福语
15 祝男孩生日祝福语简短
16 同事调离的祝福语简短
17 拜年红包的祝福语简短
18 妈妈生日祝福语简短励志