Python|迭代列表的元组列表的方法
List是一个重要的容器,几乎在日常编程和Web开发的每一个代码中都有使用,使用的越多,掌握它的要求就越多,因此了解它的操作是必要的。
给定一个列表的元组列表,编写一个Python程序来遍历它并获取所有元素。
方法 #1:使用 itertools.ziplongest
Python3
# Python code to demonstrate ways to iterate
# tuples list of list into single list
# using itertools.ziplongest
# import library
from itertools import zip_longest
# initialising listoflist
test_list = [
[('11'), ('12'), ('13')],
[('21'), ('22'), ('23')],
[('31'), ('32'), ('33')]
]
# printing initial 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)
Python3
# Python code to demonstrate
# ways to iterate tuples list of list into single 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 initial 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)
Python3
# Python code to demonstrate ways to
# iterate tuples list of list into single
# list using list comprehension
# initialising listoflist
test_list = [
[('11'), ('12'), ('13')],
[('21'), ('22'), ('23')],
[('31'), ('32'), ('33')]
]
# printing initial 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)
输出:
Initial List = [['11', '12', '13'], ['21', '22', '23'], ['31', '32', '33']]
Resultant List = ['11', '21', '31', '12', '22', '32', '13', '23', '33']
方法#2:使用 lambda + itertools.chain + zip_longest
Python3
# Python code to demonstrate
# ways to iterate tuples list of list into single 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 initial 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)
输出:
Initial List = [['11', '12', '13'], ['21', '22', '23'], ['31', '32', '33']]
Resultant List = ['11', '21', '31', '12', '22', '32', '13', '23', '33']
方法#3:使用列表理解
列表推导式是在Python中定义和创建列表的一种优雅方式。我们可以像数学语句一样创建列表,并且仅在一行中。列表推导的语法更容易掌握。
Python3
# Python code to demonstrate ways to
# iterate tuples list of list into single
# list using list comprehension
# initialising listoflist
test_list = [
[('11'), ('12'), ('13')],
[('21'), ('22'), ('23')],
[('31'), ('32'), ('33')]
]
# printing initial 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)
输出:
Initial List = [['11', '12', '13'], ['21', '22', '23'], ['31', '32', '33']]
Resultant List = ['11', '12', '13', '21', '22', '23', '31', '32', '33']