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

Function randomPickDict

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

given a diictionary d, with all values are numbers, randomly pick an item and return it's key according to the percentage of its values

(d)

Source from the content-addressed store, hash-verified

188#========================================================================
189
190def randomPickDict(d):
191 ''' given a diictionary d, with all values are numbers,
192 randomly pick an item and return it's key according
193 to the percentage of its values'''
194 ks = d.keys()
195 vs = accumList(d.values(),1)
196 return ks[ findIndex( vs, random.random()) ]
197
198#========================================================================
199

Callers

nothing calls this directly

Calls 5

accumListFunction · 0.50
findIndexFunction · 0.50
keysMethod · 0.45
valuesMethod · 0.45
randomMethod · 0.45

Tested by

no test coverage detected