Python--zip()函数

材料链接:http://www.runoob.com/python/python-func-zip.html

1. zip()函数: 

zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表。

如果各个迭代器的元素个数不一致,则返回列表长度与最短的对象相同,利用 * 号操作符,可以将元组解压为列表。

zip 方法在 Python 2 和 Python 3 中的不同:在 Python 3.x 中为了减少内存,zip() 返回的是一个对象。如需展示列表,需手动 list() 转换。

如果需要了解 Pyhton3 的应用,可以参考 Python3 zip()

2.语法

zip([iterable, ...])

参数说明:iterabl -- 一个或多个迭代器;

3. 实例

>>>a = [1,2,3]

>>> b = [4,5,6]

>>> c = [4,5,6,7,8]

>>> zipped = zip(a,b)     # 返回一个对象

>>> zipped

<zip object at 0x103abc288>

>>> list(zipped)  # list() 转换为列表

[(1, 4), (2, 5), (3, 6)]

>>> list(zip(a,c))              # 元素个数与最短的列表一致

[(1, 4), (2, 5), (3, 6)]

>>> a1, a2 = zip(*zip(a,b))          # 与 zip 相反,zip(*) 可理解为解压,返回二维矩阵式

>>> list(a1)

[1, 2, 3]

>>> list(a2)

[4, 5, 6]

>>> 

原文地址:https://www.cnblogs.com/gegemu/p/9958253.html