- set seen, , yield ed. , , iterable ( ), roundrobin.
def roundrobin_limited_nodupe(limit, *iterables):
"""A round-robin iterator duplicates removed and a limit.
>>> list(roundrobin_limited_nodupe(6, 'ABC', 'DB', 'EFG'))
['A', 'D', 'E', 'B', 'F', 'C'] # only six elements, only one 'B'
Notes:
- Recipe credited to George Sakkis
"""
pending = len(iterables)
seen = set()
nexts = cycle(iter(it).next for it in iterables)
while pending:
try:
for next in nexts:
candidate = next()
if candidate not in seen:
seen.add(candidate)
yield candidate
limit -= 1
if limit == 0:
return
except StopIteration:
pending -= 1
nexts = cycle(islice(nexts, pending))