MCPcopy Create free account
hub / github.com/ActiveState/code / normDictSumTo

Function normDictSumTo

recipes/Python/278259_Dictionary_Tools/recipe-278259.py:104–112  ·  view source on GitHub ↗

Normalize the values of d to sumTo. The returned dict will have value sum = sumTo

(d, sumTo=1)

Source from the content-addressed store, hash-verified

102#========================================================================
103
104def normDictSumTo(d, sumTo=1):
105 ''' Normalize the values of d to sumTo. The returned dict will
106 have value sum = sumTo
107 '''
108 ks = d.keys()
109 vs = d.values()
110 sum = reduce(lambda x,y:x+y, vs)
111 vs= [ x/(sum*1.0)*sumTo for x in vs]
112 return dict(zip(ks, vs))
113
114#========================================================================
115

Callers

nothing calls this directly

Calls 3

dictClass · 0.50
keysMethod · 0.45
valuesMethod · 0.45

Tested by

no test coverage detected