0

I have an array [1, 1, 2, 3, 3, 3, 0, 0, 0, 0, 0]. I want to generate all permutations under considerations of symmetry/repetition (i.e. for a smaller example: [1, 1] has only one permutation, not two for exchanging the 1s.) Enumerating all permutations with itertools (see code below) is not efficient, especially since I want to go to larger sets.

from itertools import permutations

p = permutations([1, 1, 2, 3, 3, 3, 0, 0, 0, 0, 0])
j = 0
for i in list(p):
    print(i)
    j += 1
# reduce all permutations e.g. with set().

Is there an elegant way of enumerating all combinations? How can I implement it in an efficient way (may largest case has about 100 elements in the array)? Thanks for the help.

ste
  • 448
  • 7
  • 14
  • I do not generally believe in Geeksforgeeks, still, these will help: https://www.geeksforgeeks.org/print-all-permutations-of-a-string-with-duplicates-allowed-in-input-string/ and https://www.geeksforgeeks.org/distinct-permutations-string-set-2/ – metamemelord Apr 03 '18 at 05:47
  • "may largest case has about 100 elements in the array" → You're probably doomed unless this is mostly a single repeated value. – Veedrac Apr 03 '18 at 08:46
  • These are [multisets](https://en.wikipedia.org/wiki/Multiset), and the `sympy` library is equipped to handle them. – Joseph Wood Apr 03 '18 at 12:15

0 Answers0