python zip() 同时遍历多个序列

2022-05-16  本文已影响0人  孙广宁
4.11 如果我们要遍历的元素包含在多个序列中,如何对他们同时进行遍历
>>> xpts=[1,5,3,2,6,7]
>>> ypts=[101,105,103,102,106,107]
>>> for x,y in zip(xpts,ypts):
...     print(x,y)
...
1 101
5 105
3 103
2 102
6 106
7 107
>>> ypts=[101,105,103,102,106,107,108]
>>> for x,y in zip(xpts,ypts):
...     print(x,y)
...
1 101
5 105
3 103
2 102
6 106
7 107
>>>
>>> from itertools import zip_longest
>>> for i in zip_longest(xpts,ypts):
...     print(i)
...
(1, 101)
(5, 105)
(3, 103)
(2, 102)
(6, 106)
(7, 107)
(None, 108)
>>>
上一篇 下一篇

猜你喜欢

热点阅读