python, convert a dictionary to a sorted list by value instead of key

后端 未结 7 1115
别跟我提以往
别跟我提以往 2021-02-04 03:45

I have a collections.defaultdict(int) that I\'m building to keep count of how many times a key shows up in a set of data. I later want to be able to sort it (obviously by turnin

7条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-04 04:28

    Just sort the resulting dict by values:

    for k, v in sorted(adict.items(), key=lambda kv: kv[1], reverse=True):
        print("%s => %s" % (k,v))
    

提交回复
热议问题