I need to get all possible 5 combinations from a list with the following constraints:
- Combinations must include repetitions.
- The sum of all numbers is equal to 1.
Here is my code so far:
number = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]
comb = [c for c in itertools.product(number, repeat=5) if c[0] + c[1] + c[2] + c[3] + c[4] == 1]
for cb in comp:
print(cb)
It seems I am not getting all possible combinations. For instance, the first line of output would be
(0.1, 0.1, 0.1, 0.1, 0.6)
but does not include any of the following
(0.6, 0.1, 0.1, 0.1, 0.1)
(0.1, 0.6, 0.1, 0.1, 0.1)
(0.1, 0.1, 0.6, 0.1, 0.1)
(0.1, 0.1, 0.1, 0.6, 0.1)
and so on. I also tried different approaches with
itertools.combinations_with_replacement
itertools.permutations