I want to get all the possible permutations of two different elements grouped by four without repeating the output but repeating elements (obviously you need to repeat elements to make combinations of 4 with only 2 elements).
So, some things I've tried:
sorted(list(map(lambda x: "".join(x), list(permutations('AB', 4)))))
returns an empty list. So I tried this instead:
sorted(list(map(lambda x: "".join(x), list(permutations('AABB', 4)))))
And it is closed to the output I expect, but full of redundant items, just a few of them:
['AABB','AABB','AABB','AABB','ABAB','ABAB','ABAB',...
What I expected to get is actually:
['AABB','ABAB','ABBA','BAAB','BABA','BBAA']
I tried also combinations() and product() instead of permutations() without success, but maybe my arguments weren't fit to the problem.
Any ideas?
Thanks a lot in advance!
PS. As pointed out by Antoine, there's a workaround using a set instead of a list, like:
sorted(list(map(lambda x: "".join(x), set(permutations('AABB', 4)))))
That gives the expected output, still it is generating all the repetitions anyway but just happen to be filtered out because sets cannot have repeated element, so it's probably not the most efficient way to do it.
1. Permutations of a Python string If we are given a Python string and asked to find out all the ways its letters can be arranged, then the task can easily be achieved by the permutations () function. import itertools st = "ABC" per = itertools.permutations (st) for val in per: print (*val)
If your string contains a lot of repeated characters, then you can use a combinations-based algorithm to generate your permutations. Basically, this works by choosing a letter and finding all the places where the duplicates of that letter can go. With each of those possibilities, you find all the places where the next letter goes, and so on.
Some people get confused between combinations and python permutation, in permutations the order matters but in combinations, the order doesn’t matter.
Just like you have in the comments, when Python is doing permutations, you should, and do get 720 = 10 ⋅ 9 ⋅ 8 . This means that neither 211 or 112 will not be in your list of all permutations: you have 10 options to pick from for the first digit. Thanks for contributing an answer to Mathematics Stack Exchange!
Well you can use a set
instead of a list and get the desired result
sorted(set(map(lambda x: "".join(x), list(permutations('AABB', 4)))))
But be warned that this may not work very well for very large number of elements in the bag. It doesn't scale
gives
['AABB', 'ABAB', 'ABBA', 'BAAB', 'BABA', 'BBAA']
Slightly more perfomant perhaps
sorted(map(lambda x: "".join(x), set(permutations('AABB', 4))))
The permutations equivalent python code is listed here: https://docs.python.org/2/library/itertools.html#itertools.permutations
The original code is implemented in C so it would run faster. In the end doing permutations of large numbers of items simply does not scale whether or not you leave out duplicates.
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