python zip函数_Python zip函数

This function returns a list of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. The returned list is truncated in length to the length of the shortest argument sequence. When there are multiple arguments which are all of the same length,zip()is similar tomap()with an initial argument of None. With a single sequence argument, it returns a list of 1-tuples. With no arguments, it returns an empty list.

The left-to-right evaluation order of the iterables is guaranteed. This makes possible an idiom for clustering a data series into n-length groups using zip(*[iter(s)]*n).

zip()in conjunction with the * operator can be used to unzip a list:

pythonAPI上面说的

下面自己上例子:

x=[1,2,3]

y=[4,5,6]

z=[7,8,9]printzip(x)printzip(x,y)print zip(x,y,z)

结果是:

[(1,), (2,), (3,)]

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

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

可以看出zip是把同一维度的数据放在一个list中

w=zip(x,y,z)for a,b,c inw:print 'a:{0},b:{1},c:{2}'.format(a,b,c)

结果是:

a:1,b:4,c:7a:2,b:5,c:8a:3,b:6,c:9

for是把list中的tuple中的每个元素输出

print type(w), type(w[1])

x=[1,2,3]

y=[4,5,6]

z=[7,8,9]

w=zip(x,y,z)printwprint zip(*w)

结果:

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

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

这是zip(*w)是把功能返回去而已

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值