I have a dictionary which has elements to sort as keys and their weights / probabilities as values.
I would like to get a sorted list of keys using the values as probability to be the next element chosen.
Example:
l = {6: 5859, 7: 61636, 2: 53317}
# Example output 1
output1 = [7,2,6]
# Example output 2
output2 = [2,7,6]
I researched a bit and found the sort function redefining it and multiplying random() with the weight itself, but I do not get the syntax to run. So my "pseudo code" python solution is:
from random import random
l.sort(key = lambda element: random() * element.value())
Of course, a dictionary does not have a sort function and element.value() does not work, but this is how I think it could work with better syntax.
Or is there a better solution?