python的zip函数_python的zip函数

zip()函数

它是Python的内建函数,(与序列有关的内建函数有:sorted()、reversed()、enumerate()、zip()),其中sorted()和zip()返回一个序列(列表)对象,reversed()、enumerate()返回一个迭代器(类似序列)

1 >>> type(sorted(s))

2

3 >>> type(zip(s))

4

5 >>> type(reversed(s))

6

7 >>> type(enumerate(s))

8

那么什么是zip()函数 呢?

我们help(zip)看看:

1 >>> help(zip)

2 Help on built-in function zip in module __builtin__:

3

4 zip(...)

5 zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]

6

7 Return a list of tuples, where each tuple contains the i-th element

8 from each of the argument sequences. The returned list is truncated

9 in length to the length of the shortest argument sequence.

提示:不懂的一定多help

定义:zip([seql, ...])接受一系列可迭代对象作为参数,将对象中对应的元素打包成一个个tuple(元组),然后返回由这些tuples组成的list(列表)。若传入参数的长度不等,则返回list的长度和参数中长度最短的对象相同。

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

2 >>> z2=[4,5,6]

3 >>> result=zip(z1,z2)

4 >>> result

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

6 >>> z3=[4,5,6,7]

7 >>> result=zip(z1,z3)

8 >>> result

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

10 >>>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值