接收两个或多个序列,并返回一个元组列表,并且每个元组列表包含来自每个序列中的第一个元素
s = "abc" t = [11,23,45] print(zip(s,t)) for pair in zip(s,t): print(pair) print(list(zip(s,t)))
D:PythonPython35python.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:PythonPython35python.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:PythonPython35python.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