有没有更好的方法来迭代两个列表,每次迭代从每个列表中获取一个元素? [英] Is there a better way to iterate over two lists, getting one element from each list for each iteration?
本文介绍了有没有更好的方法来迭代两个列表,每次迭代从每个列表中获取一个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个纬度列表和一个经度列表,需要遍历纬度和经度对.
I have a list of Latitudes and one of Longitudes and need to iterate over the latitude and longitude pairs.
是否更好:
A.假设列表长度相等:
A. Assume that the lists are of equal lengths:
for i in range(len(Latitudes)):
Lat,Long=(Latitudes[i],Longitudes[i])
B.或者:
B. Or:
for Lat,Long in [(x,y) for x in Latitudes for y in Longitudes]:
(注意 B 不正确.这给了我所有的对,相当于 itertools.product()
)
(Note that B is incorrect. This gives me all the pairs, equivalent to itertools.product()
)
对每个的相对优点有什么想法,或者哪个更pythonic?
Any thoughts on the relative merits of each, or which is more pythonic?
推荐答案
这就像你能得到的那样 Pythonic:
This is as pythonic as you can get:
for lat, long in zip(Latitudes, Longitudes):
print(lat, long)
这篇关于有没有更好的方法来迭代两个列表,每次迭代从每个列表中获取一个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文