[ create a new paste ] login | about

Link: http://codepad.org/v6tmqEwy    [ raw code | output | fork ]

Python, pasted on May 31:
1
2
3
4
5
6
7
8
9
def product(xs, repeat=None):
  xs_len = len(xs)
  repeat = repeat if repeat else xs_len
  for n in xrange(repeat ** xs_len):
    yield [xs[(n/(repeat**m)) % xs_len] for m in xrange(repeat-1,-1,-1)]

if __name__ == '__main__':
  for x in product([1,2,3], repeat=3):
    print x


Output:
[1, 1, 1]
[1, 1, 2]
[1, 1, 3]
[1, 2, 1]
[1, 2, 2]
[1, 2, 3]
[1, 3, 1]
[1, 3, 2]
[1, 3, 3]
[2, 1, 1]
[2, 1, 2]
[2, 1, 3]
[2, 2, 1]
[2, 2, 2]
[2, 2, 3]
[2, 3, 1]
[2, 3, 2]
[2, 3, 3]
[3, 1, 1]
[3, 1, 2]
[3, 1, 3]
[3, 2, 1]
[3, 2, 2]
[3, 2, 3]
[3, 3, 1]
[3, 3, 2]
[3, 3, 3]


Create a new paste based on this one


Comments: