Shortcut for all the possible permutation of a set of numbers for m digits

79 Views Asked by At

I have been working on finite field. Suppose I have a prime number p=7. So I get a list q=[0,1,2,3,4,5,6]. Now I want all the possible permutation of the elements of set q for 7 places. For example [1,1,1,4,6,3,1] is one of the possible permutation. Is there any inbuilt command in python for doing that? Actually I am working with bigger field where P is 127 (p=127).

1

There are 1 best solutions below

2
Jean-François Fabre On

Those aren't permutations because elements are repeated, this looks more like a product.

you can use itertools.product on repeated q lists (here for 3 elements):

import itertools

q=[0,1,2]   # or q = list(range(3))

for z in itertools.product(*(q,)*len(q)):  # using arg unpacking like if it was (q,q,q)
    z = list(z) # to convert as list
    print(z)

prints:

[0, 0, 0]
[0, 0, 1]
[0, 0, 2]
[0, 1, 0]
[0, 1, 1]
[0, 1, 2]
...snip...
[2, 2, 0]
[2, 2, 1]
[2, 2, 2]

for p=3 it prints 3**3 = 27 values. If p=127 well... sounds not reasonable.