[python] Permutation generation considering the same elements

Generating permutations containing the same elements

With itertools.permutations, extra is generated when the same element is used. It is used when handling the same numbers, the same letters, the same order, etc. I introduced it earlier in How to use the generator, but I introduced it alone because it was difficult for me to find it. github source: permutation.py

permutation.py


def permutations(iterable, permutation=[]):
    if not iterable: 
        yield permutation
        pass
    for i in [iterable.index(i) for i in set(iterable)]:
        yield from permutations(iterable[:i] + iterable[i+1:], permutation + [iterable[i]])

Is yield from recursive? In addition, it returns the yield from a distance. I set it every time to prevent duplication of the same element.

It seems to be a little slow to stick with slices, so please tell me if there is a good way.

Recommended Posts

[python] Permutation generation considering the same elements
Permutation generation
Python open and io.open are the same
[Python] Combine all the elements in the array
[python] Check the elements of the list all, any
58 The same castle
Building multiple Python environments on the same system
Get the size (number of elements) of UnionFind in Python
Python program that looks for the same file name
[Python] Outputs all combinations of elements in the list
Find the maximum Python
the zen of Python
[Python] Split the date
Sort and output the elements in the list as elements and multiples in Python.
Get the number of specific elements in a python list
Get the current date and time in Python, considering the time difference
[Python3] "A // B" and "math.floor (A / B)" are not always the same! ??