Any better way to generate a tuple from all possible tuples in the form (x1, y1, x2, y2)?
I want to generate a tuple of a tuple in the form ((x1, y1, x2, y2), ... (x1, y1, x2, y2)) where x1, y1, x2, y2 are all in the range (0.8).
Is there any other way 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
+2
a source to share
1 answer
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)))
.
+7
a source to share