Is there a better way to iterate over two lists, getting one element from each list for each iteration? [duplicate]
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:
for i in range(len(Latitudes)): Lat,Long=(Latitudes[i],Longitudes[i])
B. Or:
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()
)
Any thoughts on the relative merits of each, or which is more pythonic?
This is as pythonic as you can get:
for lat, long in zip(Latitudes, Longitudes):
print(lat, long)
Another way to do this would be to by using 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:
>>> 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
Good to see lots of love for zip
in the answers here.
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).
In python 3 and later zip
behaves like izip
.
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
from itertools import izip, imap
out = imap(process, izip(latitudes, longitudes))
Iterating through elements of two lists simultaneously is known as zipping, and python provides a built in function for it, which is documented here.
>>> 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]
In your case, it will be simply:
for (lat, lon) in zip(latitudes, longitudes):
... process lat and lon
for Lat,Long in zip(Latitudes, Longitudes):
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.
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
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.
>>> 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()
.
>>> 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]
精彩评论