# 有没有更好的方法来迭代两个列表，每个迭代从每个列表中获取一个元素？

• A.假设列表长度相同：

``for i in range(len(Latitudes): Lat,Long=(Latitudes[i],Longitudes[i])` `
• B.或者：

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

（注意B是不正确的，这给了我所有的对，相当于`itertools.product()`

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

` `>>> 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` `

` `>>> 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` `

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

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

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

` `>>> 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` `

[以pydocs为例]

` `for (lat, lon) in zip(latitudes, longitudes): ... process lat and lon` `
` `for Lat,Long in zip(Latitudes, Longitudes):` `

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

` `>>> 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]` `