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?
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 aset
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
set
s: