I know about itertools, but it seems it can only generate permutations without repetitions.
for example, I'd like to generate all possible dice rolls for 2 dice. So I need all permutations of size 2 of [1, 2, 3, 4, 5, 6] including repetitions: (1, 1), (1, 2), (2, 1)... etc
If possible I don't want to implement this from scratch
You are looking for the Cartesian Product.
In mathematics, a Cartesian product (or product set) is the direct product of two sets.
In your case, this would be {1, 2, 3, 4, 5, 6} x {1, 2, 3, 4, 5, 6}.
itertools can help you there:
import itertools
x = [1, 2, 3, 4, 5, 6]
[p for p in itertools.product(x, repeat=2)]
[(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (2, 1), (2, 2), (2, 3),
(2, 4), (2, 5), (2, 6), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6),
(4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (5, 1), (5, 2), (5, 3),
(5, 4), (5, 5), (5, 6), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6)]
To get a random dice roll (in a totally inefficient way):
import random
random.choice([p for p in itertools.product(x, repeat=2)])
(6, 3)
You're not looking for permutations - you want the Cartesian Product. For this use product from itertools:
from itertools import product
for roll in product([1, 2, 3, 4, 5, 6], repeat = 2):
print(roll)
In python 2.7 and 3.1 there is a itertools.combinations_with_replacement function:
>>> list(itertools.combinations_with_replacement([1, 2, 3, 4, 5, 6], 2))
[(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (2, 2), (2, 3), (2, 4),
(2, 5), (2, 6), (3, 3), (3, 4), (3, 5), (3, 6), (4, 4), (4, 5), (4, 6),
(5, 5), (5, 6), (6, 6)]
First, you'll want to turn the generator returned by itertools.permutations(list) into a list first. Then secondly, you can use set() to remove duplicates Something like below:
def permutate(a_list):
import itertools
return set(list(itertools.permutations(a_list)))
Here is c# version (even though its asked for python, the algorithm should be same) just for reference:
Below method basically takes no. of times the dice can be tossed to get to various permutations. For the above question, size should be '2'.
private void GetAllPermutationsOfDice_Recursive(int size, string currentValue,
List<string> values)
{
if(currentValue.Length == size)
{
values.Add(currentValue);
return;
}
for(int i = 1; i<=6;i++)
{
this.GetAllPermutationsOfDice_Recursive(size, currentValue + i, values);
}
}
To toss the dice twice, the above method can be called as:
public string[] GetAllPermutationsOfDiceOfSize_2()
{
List<string> values = new List<string>();
this.GetAllPermutationsOfDice_Recursive(2, "", values);
return values.ToArray();
}
Following are corresponding unit tests:
[TestMethod]
public void Dice_PermutationsTests()
{
var v = this.GetAllPermutationsOfDiceOfSize_2();
Assert.AreEqual(36, v.Length);
int l = 6;
List<string> values = new List<string>();
for(int i = 1; i<=4; i++)
{
values.Clear();
this.GetAllPermutationsOfDice_Recursive(i, "", values);
Assert.AreEqual(l, values.Count);
l *= 6;
}
}
来源:https://stackoverflow.com/questions/3099987/generating-permutations-with-repetitions-in-python