Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def product(*iterables):
- if iterables:
- first, *others = iterables
- for x in first:
- for xs in product(*others):
- yield [x] + xs
- else:
- yield []
- def product(*iterables):
- if not iterables:
- return
- iters = [iter(xs) for xs in iterables]
- last = len(iters)-1
- i = 0
- row = [0] * len(iters)
- while True:
- try:
- n = next(iters[i])
- except StopIteration:
- if i == 0:
- return
- iters[i] = iter(iterables[i])
- i -= 1
- else:
- row[i] = n
- if i == last:
- yield list(row)
- else:
- i += 1
Advertisement
Add Comment
Please, Sign In to add comment