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