zip(*iterables)
zip n. 拉链;一种程序压缩格式,拉链; 拉拉链
将多个迭代器的东西进行链接,生成一个可迭代的对象,每个迭代对象为一个元组,元组的内容由每个参数的对应位置的数据组成,以长度最短的元祖长度计算。如下面的zipped1中,长度为3,list1种的第四个元素就被丢弃了。每个元祖是(list1[n], list2[n], list3[n])。
list1 = [1,2,3,4]
list2 = [5,6,7]
list3 = [8,9,10]
zipped1 = zip(list1,list2,list3)
zipped2 = zip(list1)
for z in zipped1:
print(z)
print('------------')
for z in zipped2:
print(z)
输出内容为:
[(1, 5, 8), (2, 6, 9), (3, 7, 10)][(1,), (2,), (3,), (4,)]
[]
<完>