- 源码描述
- 英文描述
Return a zip object whose .next() method returns a tuple where the i-th element comes from the i-th iterable argument. The .next() method continues until the shortest iterable in the argument sequence is exhausted and then it raises StopIteration.
- 英文描述
- 有道翻译
返回一个zip对象,其中.next()方法返回一个元组
第i个元素来自第i个可迭代参数。.next()
方法继续执行,直到参数序列中最短的可迭代
耗尽,然后引发StopIteration。 - 菜鸟教程描述
zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表。
如果各个迭代器的元素个数不一致,则返回列表长度与最短的对象相同,利用 * 号操作符,可以将元组解压为列表。
注:
zip 方法在 Python 2 和 Python 3 中的不同:在 Python 3.x 中为了减少内存,zip() 返回的是一个对象。如需展示列表,需手动 list() 转换。
-
语法
zip([iterable, …])
#iterable指迭代器
#[iterable, …]指一个或者多个迭代器''' 返回值:元组列表(列表里面套元组) '''
实例:
a = [1,2,3]
b = [4,5,6]
c = [4,5,6,7,8]
zipped1 = zip(a,b) #打包为元组的列表
print(zipped1)
zipped2 = zip(a,c) #元素个数与最短的列表一致
print(zipped2)
print(*zipped)
#与zip相反,*zipped可以理解为解压,返回二维矩阵