python中的zip()函数
当学到组合数据类型之一——字典时,书上有一个创建字典的方法如下:
dict1 = dict(zip(a,b))
今天复习的时候发现,zip的参数不局限于列表和元组或者集合,只要是个迭代器都可以:
import itertools as it
a = ['星期一','星期二','星期三']
dict1 = dict(zip(range(10),it.cycle(a)))
print(dict1)
接着我发现zip()函数绝不仅仅用于创建字典,这只是其中一种功能
这是zip()函数对应的python源码:
def zip(seq1, seq2, *more_seqs): # known special case of zip
"""
zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]
Return a list of tuples, where each tuple contains the i-th element
from each of the argument sequences. The returned list is truncated
in length to the length of the shortest argument sequence.
"""
pass