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

后端 未结 7 1117
别跟我提以往
别跟我提以往 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:22

    To get the dictionary sorted:

    from operator import itemgetter
    
    sorted(adict.iteritems(), key=itemgetter(1), reverse=True)
    

提交回复
热议问题