Ask Your Question

Revision history [back]

There is a nice module named itertools for sich things, here is how you can use product it provides in you context:

As an example, you can replace nested loops:

sage: for i0 in range(1,5):
....:     for i1 in range(1,5):
....:         for i2 in range(1,5):
....:             print('{} sdf {} fds {}'.format(i0, i1, i2))
....:             print('the sum is {}'.format(i0 + i1 + i2))

with a single loop over the product:

sage: import itertools
sage: for i in itertools.product(range(1,5), repeat=3):
....:     print('{} sdf {} fds {}'.format(*i))
....:     print('the sum is {}'.format(i[0] + i[1] + i[2]))