I want to create all the possible combinations of a tuple with duplicate elements.
For example lets say that we have {3,3,0}. I want the code to return: (3,3,0) (3,0,3) (0,3,3)
I looked at itertools documentation and at numerous stack overflow questions but i did not find a answer to this.
How can I implement it? Or, can this be implemented in general?
You could use permutations from itertools and then use a set to remove any doubles:
result = set(permutations((3, 3, 0)))
Using more_itertools, a third-party library:
> pip install more_itertools
Code
import more_itertools as mit
iterable = (3, 3, 0)
list(mit.distinct_permutations(iterable))
# [(3, 3, 0), (3, 0, 3), (0, 3, 3)]
See more_itertools docs for details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With