You should use the itertools module that already has the algorithms to do most of what you want.
from itertools import combinations
def valid_combinations(weights):
'''generator of possible combinations of weights elements that add up to 1'''
list_length = len(weights) # we will need this
for lengths in range(list_length):
for possible in combinations(weights, lengths): # all possible orderings of weights
if sum(possible[:lengths]) == 1: # only generate valid ones
yield possible[:lengths]
>>> original = [0, .1, .2, .3, .4, .5]
>>> print list(valid_combinations(original))
[(0.1, 0.4, 0.5), (0.2, 0.3, 0.5), (0, 0.1, 0.4, 0.5), (0, 0.2, 0.3, 0.5), (0.1, 0.2, 0.3, 0.4), (0, 0.1, 0.2, 0.3, 0.4)]
If you are only interested in unique combinations of the weights (the order does not matter), you need to use combinations
, if it does matter you should use permutations
the following way:
from itertools import permutations
def valid_combinations(weights):
'''generator of possible combinations of weights elements that add up to 1'''
list_length = len(weights) # we will need this
for possible in permutations(weights): # all possible orderings of weights
for lengths in range(list_length): # get all prefix sublists
if sum(possible[:lengths]) == 1: # only generate valid ones
yield possible[:lengths]
>>> original = [0, .1, .2, .3, .4, .5]
>>> print list(valid_combinations(original))
>>> [(0, 0.1, 0.2, 0.3, 0.4), (0, 0.1, 0.2, 0.4, 0.3), (0, 0.1, 0.3, 0.2, 0.4), (0, 0.1, 0.3, 0.4, 0.2), (0, 0.1, 0.4, 0.2, 0.3), (0, 0.1, 0.4, 0.3, 0.2), (0, 0.1, 0.4, 0.5), (0, 0.1, 0.4, 0.5), (0, 0.1, 0.5, 0.4), (0, 0.1, 0.5, 0.4), (0, 0.2, 0.1, 0.3, 0.4), (0, 0.2 ...