tags:

views:

97

answers:

1

I want to generate a tuple of tuple in form of ((x1,y1,x2,y2),...(x1,y1,x2,y2)) where x1,y1,x2,y2 are all in range of (0,8).

Is there any other way rather than the following?

S = list()
for x1 in range(0, 8):
    for y1 in range(0, 8):
        for x2 in range(0, 8):
            for y2 in range(0, 8):
                S.append([x1,y1,x2,y2])
S = tuple(S)       

thanks

+5  A: 
tuple([x1, y1, x2, y2] for x1 in range(0, 8) for x2 in range(0, 8) for y1 in range(0, 8) for y2 in range(0, 8))

Or

import itertools
a = [range(0,8)]*4
print tuple(itertools.product(*a))

Note that this returns a tuple of tuples. If you need a tuple of lists, use tuple(itertools.imap(list, itertools.product(*a))).

KennyTM
+1 for itertools.
Aaron Digulla
itertools to the rescue, +1
Agos
You can also use the `repeat` keyword argument to good effect: `tuple(itertools.product(range(8), repeat=4))`
Mark Dickinson
The next version of itertools will include `itertools.solve_traveling_salesman()` and `itertools.fermats_last_theorem()`.
Paul McGuire
@Paul: I certainly wish that the [recipes](http://docs.python.org/library/itertools.html#recipes) were included before your suggested additions, but hey! everyone is entitled to their dreams.
ΤΖΩΤΖΙΟΥ