温馨提示×

python的zip函数怎么导入

九三
176
2021-02-14 08:36:22
栏目: 编程语言

python的zip函数怎么导入

在python中导入zip函数的方法

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

zip()函数语法:

zip([iterable, ...])

使用方法:

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

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

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

>>> zipped = zip(a,b) # 打包为元组的列表

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

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

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

>>> zip(*zipped) # 与 zip 相反,*zipped 可理解为解压,返回二维矩阵式

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

0