I have a dictionary
: keys are strings, values are float.
Example:
A = {'a':1, 'b':2, 'c':2, 'd':0}
I'd like to get 'b'
or 'c'
as answer with equal probability. I found a way to get said behaviour. However I'm not convinced this is best practice.
import random
A = {'a':1, 'b':2, 'c':2, 'd':0}
all = (A.items())
values = [(x[1],random.random(),x[0]) for x in all]
maxIndex = values.index(max(values))
print(values[maxIndex][2])
Is there a better (or even more elegant) approach?