Common items in two Python generators

2019-08-02 14:56发布

Is there a way to find common items in two Python generators, besides reading one into a list? You can't assume anything about the ordering of the items.

As a bad example:

import random
a = (random.randint(1, 50000) for _ in xrange(300))
b = (random.randint(3500, 3700) for _ in xrange(50))      

# do A and B have any elements in common?

1条回答
走好不送
2楼-- · 2019-08-02 15:32

If you can't assume anything about the order of the items, then you can't logically do this without reading one of the generators entirely into a list (or a set which might make more sense if you don't care about duplicates within one generator).

To illustrate this, let's assume that the only two identical elements were the first item of the one generator and the last item of the other generator (but you don't know which is which). You need to have exhausted one of the generators entirely to make sure you know which common elements there are.

How to do it with sets:

>>> import random
>>> a = (random.randint(1, 50000) for _ in xrange(300))
>>> b = (random.randint(3500, 3700) for _ in xrange(50))
>>> set(a).intersection(set(b))
set([])
>>> a = (random.randint(1, 50000) for _ in xrange(300))
>>> b = (random.randint(3500, 3700) for _ in xrange(50))
>>> set(a).intersection(set(b))
set([3634])
查看更多
登录 后发表回答