Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a more elegant way of getting permutations with replacement in python?

I currently want all permutations of a set of elements with replacement.

Example:

elements = ['a', 'b']

permutations with replacement = 

[('a', 'a', 'a'),
 ('a', 'a', 'b'),
 ('a', 'b', 'a'),
 ('a', 'b', 'b'),
 ('b', 'a', 'a'),
 ('b', 'a', 'b'),
 ('b', 'b', 'a'),
 ('b', 'b', 'b')]

The only way I have been able to do this is so far is with itertools.product as follows:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import itertools as it

sample_space = ['a', 'b']

outcomes = it.product(sample_space, sample_space, sample_space)
list(outcomes)

I am just wondering if there is a better way to do this as it obvious that this can get unwieldy and error prone as the sample space and required length gets larger

was expecting to find something along the lines of itertools.permutations(['a', 'b'], length=3, replace=True) maybe?

I tried itertools.permutations but the only arguments are iterable, and r which is the length required.

The output for the above example using it.permutations(sample_space, 3) would be an empty list []

>Solution :

If you’re sampling with replacement, what you get is by definition not a permutation (which just means "rearrangement") of the set. So I wouldn’t look to the permutations function for this. product is the right thing; e.g. itertools.product(['a','b'], repeat=3).

Note that if you sample from a two-element set with replacement N times, then you have effectively created an N-digit binary number, with all the possibilities that go with it. You’ve just swapped in ‘a’ and ‘b’ for 0 and 1.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading