码迷,mamicode.com
首页 > 编程语言 > 详细

【python】内置函数zip

时间:2017-04-10 22:28:08      阅读:518      评论:0      收藏:0      [点我收藏+]

标签:python   并且   pair   with   abc   div   logs   src   .exe   

接收两个或多个序列,并返回一个元组列表,并且每个元组列表包含来自每个序列中的第一个元素

s = "abc"
t = [11,23,45]
print(zip(s,t))
for pair in zip(s,t):
    print(pair)

print(list(zip(s,t)))

D:\Python\Python35\python.exe D:/PycharmProjects/helloworld/src/zipTest.py
<zip object at 0x00000201D6459648>
(‘a‘, 11)
(‘b‘, 23)
(‘c‘, 45)
[(‘a‘, 11), (‘b‘, 23), (‘c‘, 45)]

Process finished with exit code 0

zip的序列长度不同,则取短的那个。

数量不相同:t长度大于s

s = "abc"
t = [11,23,45,77,88]
print(zip(s,t))
for pair in zip(s,t):
    print(pair)

print(list(zip(s,t)))

D:\Python\Python35\python.exe D:/PycharmProjects/helloworld/src/zipTest.py
<zip object at 0x00000252DE5B96C8>
(a, 11)
(b, 23)
(c, 45)
[(a, 11), (b, 23), (c, 45)]

Process finished with exit code 0

数量不相同,s长度大于t

s = "abcer"
t = [11,23,45]
print(zip(s,t))
for pair in zip(s,t):
    print(pair)

print(list(zip(s,t)))

D:\Python\Python35\python.exe D:/PycharmProjects/helloworld/src/zipTest.py
<zip object at 0x0000024C7E2C9648>
(a, 11)
(b, 23)
(c, 45)
[(a, 11), (b, 23), (c, 45)]

Process finished with exit code 0

 

【python】内置函数zip

标签:python   并且   pair   with   abc   div   logs   src   .exe   

原文地址:http://www.cnblogs.com/AlexBai326/p/6691184.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!