关于zip
zip遍历两个长度相等的可迭代对象
names = ["John", "Tom", "Json"]
counts = [81, 94, 78]
for name, count in zip(names, counts): # 惰性生成
print(name, count)
# John 81
# Tom 94
# Json 78
最短的耗尽,zip就会终止
names = ["John", "Tom", "Json", "Dave"]
counts = [81, 94, 78]
for name, count in zip(names, counts):
print(name, count)
# John 81
# Tom 94
# Json 78
zip_longest
会遍历完每个可迭代对象
from itertools import zip_longest
names = ["John", "Tom", "Json", "Dave"]
counts = [81, 94, 78]
for name, count in zip_longest(names, counts):
print(name, count)
# John 81
# Tom 94
# Json 78
# Dave None