I am trying to generate a list of ordered pairs with replacement (i.e. needs (0.1, 0.1)
, (0.1, 0.2)
and (0.2, 0.1)
)that fulfil the condition that their sum is <= max_sum
in Python. For example, given max_sum=1
and the list:
[0, 0.1, 0.2, ..., 0.9, 1.0]
I'd like to produce a list containing:
[(0, 0), (0, 0.1), ..., (0, 0.9), (0, 1.0), (0.1, 0), (0.1, 0.1), ..., (0.1, 0.8), (0.1, 0.9), (0.2, 0), (0.2, 0.1), ...]
but not (0.1, 1.0)
etc.
Here is a likely inefficient and ugly solution that has some issues in that the if statement sometimes doesn't resolve correctly:
alphas = [0., .1, .2, .3, .4, .5, .6, .7, .8, .9, 1.]
max_sum = 1
def get_pairs(alphas, max_sum)
for a in alphas:
for b in alphas:
if a + b <= max_sum:
yield (a, b)