Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Permutations without repeating the output in Python [duplicate]

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.

like image 611
Fran Marzoa Avatar asked Jun 09 '17 13:06

Fran Marzoa


People also ask

How do you find all permutations of a string in Python?

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)

How do you generate all possible permutations of a string?

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.

What is the difference between Python permutation and combinations?

Some people get confused between combinations and python permutation, in permutations the order matters but in combinations, the order doesn’t matter.

Is there a list of all permutations of 211 in Python?

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!


1 Answers

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.

like image 91
e4c5 Avatar answered Nov 14 '22 22:11

e4c5