是否有更好的方法来迭代两个列表,每次迭代从每个列表中获取一个元素? [英] Is there a better way to iterate over two lists, getting one element from each list for each iteration?

查看:109
本文介绍了是否有更好的方法来迭代两个列表,每次迭代从每个列表中获取一个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有纬度和经度之一的列表,需要迭代纬度和经度对。

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屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆