# Generating all combinations

pataphor pataphor at gmail.com
Mon Jun 1 11:11:00 EDT 2009

```Johannes Bauer wrote:

> Any help is appreciated!

This is on the fringe of exploitation, but hey, maybe the code helps you

IMHO the following code is a glaring complaint about the injustice of
omission itertools inflicts on the perfectly natural and obvious
procedure of repeat_each (whatever it's name ought to be):

from itertools  import izip, islice, cycle

def repeat_each(seq,n):
while True:
for x in seq:
for i in range(n):
yield x

def repeat_all(seq,n):
while True:
for i in range(n):
for x in seq:
yield x

def product(X):
N = []
total = 1
for x in X:
N.append(total)
total *= len(x)
R = [repeat_all(repeat_each(x,k),n)
for x,k,n in izip(X,N,reversed(N))]
return islice(izip(*R),total)

def test1():
L = ['a', 'bc','def' ]
for x in product(L):
print x
print

def test2():
repeat_all = cycle
test1()

if __name__ == '__main__':
test1()
test2()

See? Repeat_all and repeat_each are almost brothers, just separated by
the tiniest rearrangement of their genetic code (or should I say code
genetics?). Yet one is included as 'itertools.cycle' and the other is
doomed to live in limbo. Such injustice! Can it be that
'itertools.repeat' has usurped repeat_each's rightful position?

P.

```