首页 > 编程 > Python > 正文

python的zip()函数

2019-11-08 01:09:32
字体:
来源:转载
供稿:网友

文档 意思是zip(*list),list是列表,zip()返回的是一个元组。zip()函数的功能,就是从多个列表中,依次各取出一个元素。每次取出的(来自不同列表的)元素合成一个元组,合并成的元组放入zip()返回的列表中。zip()函数起到了聚合列表的功能。

"""Docstring:zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]Return a list of tuples, where each tuple contains the i-th elementfrom each of the argument sequences. The returned list is truncatedin length to the length of the shortest argument sequence.Type: builtin_function_or_method"""

用法

如果你多个等长的序列,然后想要每次循环时从各个序列分别取出一个元素,可以利用zip()方便地实现:

这里写图片描述

当然可以将聚合的元组“解开” 这里写图片描述


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表