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

Time:12-24

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:

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 []

CodePudding user response:

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.

  • Related