Python: Create a list (at N length) of all possible proportions of 1 incremented by d decimal points -


i want make list of floats of n length total sum 1 , each float incremented d amount.

for example, let n = 4 , d = 0.01:

the desired outcome so:

[0.90, 0.02, 0.03, 0.05] [0.89, 0.02, 0.03, 0.06] .... # , continues until possible sums of 1 above settings reached. # individual value can not = 0 

i'm trying think of logic need help. had asked question of similar nature, uses random numbers instead of possible combinations.

i think itertools makes pretty easy:

from itertools import permutations, ifilter  n = 4 d = 0.01 d = int(1/d)      perms = ifilter(lambda x: sum(x) < d, permutations(range(1,d-1), n-1)) tuples = [] in perms:     tuples.append(i + (d-sum(i),))  mylist = [ [ i*d in eachtuple ] eachtuple in tuples ]  print mylist 

i had assumed d 10^(-n) integer n, work integer reciprocal (0.05 , 0.2 should work, not 0.03).

floats being floats, floating-point errors, control appropriate round inside nested list comprehension.


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -