def gen( N, n ): res = [] i = 0 while N > 1: r = randint(1,n) N /= r res.append(r) i += 1 return res def abba( lst, item ): return [ item, *lst, item] def abab( lst, item ): return [ item, *lst, item, *lst] def baa( lst, item ): return [ *lst, item, item ] def ab( lst, item ): return [ item, *lst ] def ba( lst, item ): return [ *lst, item ] def struct( lst ): fnList = ['abba','abab','baa','ab','ba'] ret = [] for i,v in enumerate(lst): fn_name = choice(fnList) fn = globals().copy().get(fn_name) ret = fn(ret,{i:v}) return ret L = gen(randint(12,20),randint(3,5)) C = struct(L) R = [] Rsum = 0 Ret = [] for i in C : R.append( 1/ sqrt(sum(i.values()) )) Rsum = sum(R) for i,j in enumerate(C) : length = R[i] * duration / Rsum print(i,j,length) for id in j.keys(): print(id) Ret.append( { id : [j[id],length]} )