1

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).

rawwar
  • 4,834
  • 9
  • 32
  • 57
  • Can you explain what you mean by `permutation of the elements of set q for 7 places`? `[1,1,1,4,6,3,1] ` is not a permutation of `[0,1,2,3,4,5,6]`. – iacob Jun 13 '18 at 13:37
  • 2
    the problem is that with 127 numbers you have 127**127 cases. Just not possible to process in a reasonable time. – Jean-François Fabre Jun 13 '18 at 13:42

1 Answers1

1

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.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219