发布时间:2019-09-27 07:06:38编辑:auto阅读(1544)
zip函数接受任意多个可迭代对象作为参数,将对象中对应的元素打包成一个tuple
,然后返回一个可迭代的zip对象.
这个可迭代对象可以使用循环的方式列出其元素
若多个可迭代对象的长度不一致,则所返回的列表与长度最短的可迭代对象相同.
例1:
>>> a1=[1,2,3] >>> a2=[4,5,6] >>> a3=[7,8,9] >>> a4=["a","b","c","d"] >>> zip1=zip(a1,a2,a3) >>> print(zip1) <zip object at 0x7f5a22651c08> >>> for i in zip1: ... print(i) ... (1, 4, 7) (2, 5, 8) (3, 6, 9)
例2:
>>> zip2=zip(a1,a2,a4) >>> print(zip2) <zip object at 0x7f5a22651d48> >>> for j in zip2: ... print(j) ... (1, 4, 'a') (2, 5, 'b') (3, 6, 'c')
例3:
>>> zip3=zip(a4) >>> print(zip3) <zip object at 0x7f5a22651d08> >>> for i in zip3: ... print(i) ... ('a',) ('b',) ('c',) ('d',)
例4:
>>> zip4=zip(*a4 *3) >>> >>> print(zip4) <zip object at 0x7f5a22651f08> >>> for j in zip4: ... print(j) ... ('a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd')
>>> l1=[[1,2,3],[4,5,6],[7,8,9]] >>> print([[j[i] for j in l1] for i in range(len(l1[0])) ]) [[1, 4, 7], [2, 5, 8], [3, 6, 9]] >>> zip(*l1) <zip object at 0x7f5a22651f88> >>> for i in zip(*l1): ... print(i) ... (1, 4, 7) (2, 5, 8) (3, 6, 9)
上一篇: python3 列表元祖操作
下一篇: Python基础--Python3基础语
47750
46254
37136
34643
29233
25893
24762
19866
19428
17917
5720°
6324°
5843°
5892°
6993°
5831°
5851°
6365°
6319°
7684°