Python 2.5 dictionary 2 key sort

后端 未结 6 1752
灰色年华
灰色年华 2021-01-01 19:47

I have a dictionary of 200,000 items (the keys are strings and the values are integers).

What is the best/most pythonic way to print the items sorted by descending v

6条回答
  •  一整个雨季
    2021-01-01 20:32

    data = { 'keyC':1, 'keyB':2, 'keyA':1 }
    
    for key, value in sorted(data.items(), key=lambda x: (-1*x[1], x[0])):
        print key, value
    

提交回复
热议问题