How to iterate through two lists in parallel?

2018-12-30 23:34发布

I have two iterables in Python, and I want to go over them in pairs:

foo = (1, 2, 3)
bar = (4, 5, 6)

for (f, b) in some_iterator(foo, bar):
    print "f: ", f, "; b: ", b

It should result in:

f: 1; b: 4
f: 2; b: 5
f: 3; b: 6

One way to do it is to iterate over the indices:

for i in xrange(len(foo)):
    print "f: ", foo[i], "; b: ", b[i]

But that seems somewhat unpythonic to me. Is there a better way to do it?

8条回答
人气声优
2楼-- · 2018-12-30 23:44

The builtin zip does exactly what you want. If you want the same over iterables instead of lists you could look at itertools.izip which does the same thing but gives results one at a time.

查看更多
梦寄多情
3楼-- · 2018-12-30 23:45
def ncustom_zip(seq1,seq2,max_length):
        length= len(seq1) if len(seq1)>len(seq2) else len(seq2) if max_length else len(seq1) if len(seq1)<len(seq2) else len(seq2)
        for i in range(length):
                x= seq1[i] if len(seq1)>i else None  
                y= seq2[i] if len(seq2)>i else None
                yield x,y


l=[12,2,3,9]
p=[89,8,92,5,7]

for i,j in ncustom_zip(l,p,True):
        print i,j
for i,j in ncustom_zip(l,p,False):
        print i,j
查看更多
余欢
4楼-- · 2018-12-30 23:53

What you're looking for is called zip.

查看更多
泛滥B
5楼-- · 2018-12-30 23:54
for f, b in zip(foo, bar):
    print(f, b)

zip stops when the shorter of foo or bar stops.

In Python 2, zip returns a list of tuples. This is fine when foo and bar are not massive. If they are both massive then forming zip(foo,bar) is an unnecessarily massive temporary variable, and should be replaced by itertools.izip or itertools.izip_longest, which returns an iterator instead of a list.

import itertools
for f,b in itertools.izip(foo,bar):
    print(f,b)
for f,b in itertools.izip_longest(foo,bar):
    print(f,b)

izip stops when either foo or bar is exhausted. izip_longest stops when both foo and bar are exhausted. When the shorter iterator(s) are exhausted, izip_longest yields a tuple with None in the position corresponding to that iterator. You can also set a different fillvalue besides None if you wish. See here for the full story.

In Python 3, zip returns an iterator of tuples, like itertools.izip in Python2. To get a list of tuples, use list(zip(foo, bar)). And to zip until both iterators are exhausted, you would use itertools.zip_longest.


Note also that zip and its zip-like brethen can accept an arbitrary number of iterables as arguments. For example,

for num, cheese, color in zip([1,2,3], ['manchego', 'stilton', 'brie'], 
                              ['red', 'blue', 'green']):
    print('{} {} {}'.format(num, color, cheese))

prints

1 red manchego
2 blue stilton
3 green brie
查看更多
牵手、夕阳
6楼-- · 2018-12-30 23:58

You should use 'zip' function. Here is an example how your own zip function can look like

def custom_zip(seq1, seq2):
    it1 = iter(seq1)
    it2 = iter(seq2)
    while True:
        yield next(it1), next(it2)
查看更多
看淡一切
7楼-- · 2018-12-30 23:59

You want the zip function.

for (f,b) in zip(foo, bar):
    print "f: ", f ,"; b: ", b
查看更多
登录 后发表回答