有没有更好的方法迭代两个列表,在每个迭代中从每个列表中获取一个元素?(复制)

时间:2022-09-28 14:33:18

This question already has an answer here:

这个问题已经有了答案:

I have a list of Latitudes and one of Longitudes and need to iterate over the latitude and longitude pairs.

我有一个纬度和经度的列表,需要遍历纬度和经度对。

Is it better to:

是更好吗:

  • A. Assume that the lists are of equal lengths:

    A.假设列表的长度相等:

    for i in range(len(Latitudes):
        Lat,Long=(Latitudes[i],Longitudes[i])
    
  • B. Or:

    b或者:

    for Lat,Long in [(x,y) for x in Latitudes for y in Longitudes]:
    

(Note that B is incorrect. This gives me all the pairs, equivalent to itertools.product())

(注意B不正确。这给了我所有的对,相当于itertools.product()

Any thoughts on the relative merits of each, or which is more pythonic?

对它们的相对优点有什么想法吗,或者哪种更符合毕达哥拉斯哲学?

7 个解决方案

#1


200  

This is as pythonic as you can get:

这是毕达哥拉斯式的

for lat, long in zip(Latitudes, Longitudes):
    print lat, long

#2


42  

Another way to do this would be to by using map.

另一种方法是使用map。

>>> a
[1, 2, 3]
>>> b
[4, 5, 6]
>>> for i,j in map(None,a,b):
    ...   print i,j
    ...
1 4
2 5
3 6

One difference in using map compared to zip is, with zip the length of new list is
same as the length of shortest list. For example:

使用map与zip的一个区别是,使用zip时,新列表的长度与最短列表的长度相同。例如:

>>> a
[1, 2, 3, 9]
>>> b
[4, 5, 6]
>>> for i,j in zip(a,b):
    ...   print i,j
    ...
1 4
2 5
3 6

Using map on same data:

在相同数据上使用地图:

>>> for i,j in map(None,a,b):
    ...   print i,j
    ...

    1 4
    2 5
    3 6
    9 None

#3


21  

Good to see lots of love for zip in the answers here.

很高兴在这里看到很多对zip的爱。

However it should be noted that if you are using a python version before 3.0, the itertools module in the standard library contains an izip function which returns an iterable, which is more appropriate in this case (especially if your list of latt/longs is quite long).

但是,应该注意的是,如果您在3.0之前使用python版本,那么标准库中的itertools模块包含一个izip函数,该函数返回一个可迭代的函数,这在本例中更为合适(特别是如果您的latt/longs列表相当长)。

In python 3 and later zip behaves like izip.

在python 3和以后的zip中,zip的行为类似于izip。

#4


15  

in case your Latitude and Longitude lists are large and lazily loaded:

如果您的纬度和经度列表很大,并且加载缓慢:

from itertools import izip
for lat, lon in izip(latitudes, longitudes):
    process(lat, lon)

or if you want to avoid the for-loop

或者如果你想避免for循环

from itertools import izip, imap
out = imap(process, izip(latitudes, longitudes))

#5


6  

Iterating through elements of two lists simultaneously is known as zipping, and python provides a built in function for it, which is documented here.

同时遍历两个列表的元素称为zipping, python为它提供了一个内置函数,本文对此进行了说明。

>>> x = [1, 2, 3]
>>> y = [4, 5, 6]
>>> zipped = zip(x, y)
>>> zipped
[(1, 4), (2, 5), (3, 6)]
>>> x2, y2 = zip(*zipped)
>>> x == list(x2) and y == list(y2)
True

[Example is taken from pydocs]

[示例取自pydocs]

In your case, it will be simply:

就你而言,它将是简单的:

for (lat, lon) in zip(latitudes, longitudes):
    ... process lat and lon

#6


5  

for Lat,Long in zip(Latitudes, Longitudes):

#7


3  

This post helped me with zip(). I know I'm a few years late, but I still want to contribute. This is in Python 3.

这篇文章帮助我写zip()。我知道我晚了几年,但我仍然想要有所贡献。这是在Python 3中。

Note: in python 2.x, zip() returns a list of tuples; in Python 3.x, zip() returns an iterator. itertools.izip() in python 2.x == zip() in python 3.x

注意:在python 2。x, zip()返回元组列表;Python 3。x, zip()返回一个迭代器。在python 2 itertools.izip()。在python 3.x中x == zip()

Since it looks like you're building a list of tuples, the following code is the most pythonic way of trying to accomplish what you are doing.

由于看起来您正在构建一个元组列表,下面的代码是实现您所做事情的最python化的方式。

>>> lat = [1, 2, 3]
>>> long = [4, 5, 6]
>>> tuple_list = list(zip(lat, long))
>>> tuple_list
[(1, 4), (2, 5), (3, 6)]

Or, alternatively, you can use list comprehensions (or list comps) should you need more complicated operations. List comprehensions also run about as fast as map(), give or take a few nanoseconds, and are becoming the new norm for what is considered Pythonic versus map().

或者,如果需要更复杂的操作,也可以使用列表理解(或列表编译)。列表理解的速度也和map()一样快,只需几纳秒,并且正在成为python对map()的新规范。

>>> lat = [1, 2, 3]
>>> long = [4, 5, 6]
>>> tuple_list = [(x,y) for x,y in zip(lat, long)]
>>> tuple_list
[(1, 4), (2, 5), (3, 6)]
>>> added_tuples = [x+y for x,y in zip(lat, long)]
>>> added_tuples
[5, 7, 9]

#1


200  

This is as pythonic as you can get:

这是毕达哥拉斯式的

for lat, long in zip(Latitudes, Longitudes):
    print lat, long

#2


42  

Another way to do this would be to by using map.

另一种方法是使用map。

>>> a
[1, 2, 3]
>>> b
[4, 5, 6]
>>> for i,j in map(None,a,b):
    ...   print i,j
    ...
1 4
2 5
3 6

One difference in using map compared to zip is, with zip the length of new list is
same as the length of shortest list. For example:

使用map与zip的一个区别是,使用zip时,新列表的长度与最短列表的长度相同。例如:

>>> a
[1, 2, 3, 9]
>>> b
[4, 5, 6]
>>> for i,j in zip(a,b):
    ...   print i,j
    ...
1 4
2 5
3 6

Using map on same data:

在相同数据上使用地图:

>>> for i,j in map(None,a,b):
    ...   print i,j
    ...

    1 4
    2 5
    3 6
    9 None

#3


21  

Good to see lots of love for zip in the answers here.

很高兴在这里看到很多对zip的爱。

However it should be noted that if you are using a python version before 3.0, the itertools module in the standard library contains an izip function which returns an iterable, which is more appropriate in this case (especially if your list of latt/longs is quite long).

但是,应该注意的是,如果您在3.0之前使用python版本,那么标准库中的itertools模块包含一个izip函数,该函数返回一个可迭代的函数,这在本例中更为合适(特别是如果您的latt/longs列表相当长)。

In python 3 and later zip behaves like izip.

在python 3和以后的zip中,zip的行为类似于izip。

#4


15  

in case your Latitude and Longitude lists are large and lazily loaded:

如果您的纬度和经度列表很大,并且加载缓慢:

from itertools import izip
for lat, lon in izip(latitudes, longitudes):
    process(lat, lon)

or if you want to avoid the for-loop

或者如果你想避免for循环

from itertools import izip, imap
out = imap(process, izip(latitudes, longitudes))

#5


6  

Iterating through elements of two lists simultaneously is known as zipping, and python provides a built in function for it, which is documented here.

同时遍历两个列表的元素称为zipping, python为它提供了一个内置函数,本文对此进行了说明。

>>> x = [1, 2, 3]
>>> y = [4, 5, 6]
>>> zipped = zip(x, y)
>>> zipped
[(1, 4), (2, 5), (3, 6)]
>>> x2, y2 = zip(*zipped)
>>> x == list(x2) and y == list(y2)
True

[Example is taken from pydocs]

[示例取自pydocs]

In your case, it will be simply:

就你而言,它将是简单的:

for (lat, lon) in zip(latitudes, longitudes):
    ... process lat and lon

#6


5  

for Lat,Long in zip(Latitudes, Longitudes):

#7


3  

This post helped me with zip(). I know I'm a few years late, but I still want to contribute. This is in Python 3.

这篇文章帮助我写zip()。我知道我晚了几年,但我仍然想要有所贡献。这是在Python 3中。

Note: in python 2.x, zip() returns a list of tuples; in Python 3.x, zip() returns an iterator. itertools.izip() in python 2.x == zip() in python 3.x

注意:在python 2。x, zip()返回元组列表;Python 3。x, zip()返回一个迭代器。在python 2 itertools.izip()。在python 3.x中x == zip()

Since it looks like you're building a list of tuples, the following code is the most pythonic way of trying to accomplish what you are doing.

由于看起来您正在构建一个元组列表,下面的代码是实现您所做事情的最python化的方式。

>>> lat = [1, 2, 3]
>>> long = [4, 5, 6]
>>> tuple_list = list(zip(lat, long))
>>> tuple_list
[(1, 4), (2, 5), (3, 6)]

Or, alternatively, you can use list comprehensions (or list comps) should you need more complicated operations. List comprehensions also run about as fast as map(), give or take a few nanoseconds, and are becoming the new norm for what is considered Pythonic versus map().

或者,如果需要更复杂的操作,也可以使用列表理解(或列表编译)。列表理解的速度也和map()一样快,只需几纳秒,并且正在成为python对map()的新规范。

>>> lat = [1, 2, 3]
>>> long = [4, 5, 6]
>>> tuple_list = [(x,y) for x,y in zip(lat, long)]
>>> tuple_list
[(1, 4), (2, 5), (3, 6)]
>>> added_tuples = [x+y for x,y in zip(lat, long)]
>>> added_tuples
[5, 7, 9]