python - How to sort a dict by values and return a list of formatted strings? -
i've got dict:
text_to_count = { "text1": 1, "text2":0, "text3":2}
i'd create list of formatted strings sorting dict's values (in descending order).
i.e., following list:
result = ["2 - text3", "1 - text1", "0 - text2"]
any ideas?
edit:
while waiting responses, kept hacking @ , came with:
result = map(lambda x: "{!s} - {!s}".format(x[1], x[0]), sorted(text_to_count.iteritems(), key = lambda(k, v): (v, k), reverse=true ))
tho i'm still interested in seeing other solutions there are, possibly 1 better.
how's this?
result = ['{1} - {0}'.format(*pair) pair in sorted(text_to_count.iteritems(), key = lambda (_,v): v, reverse = true)]
Comments
Post a Comment