[oldboy-django][5python基础][内置函数]zip
python3中,把两个或两个以上的迭代器封装成生成器,在循环遍历生成器中,不断产生元组。
如果提供的迭代器长度不对等,生成器的长度为最短迭代器的长度。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# coding= utf-8 # zip 多个可迭代对象同时跑,然后各自的值组合起来 # 长度不对等情况 L1 = [1, 2, 3] L2 = 'abcd' z = zip(L1, L2) print(list(z)) # 输出[(1, 'a'), (2, 'b'), (3, 'c')] # 长度对等 L1 = [1, 2, 3] L2 = 'ccd' z = zip(L1, L2) print(list(z)) # 输出[(1, 'c'), (2, 'c'), (3, 'd')] # 变量zip生成器 name = ['Celia', 'Lisa', 'Marie'] len_name = [len(n) for n in name] for name, len_name in zip(name, len_name): print(name, len_name)