python 中字典對象按照 value 排序

dit = {1:’a’, -1:’b’, 2:’c’, -2:’d’} 直接使用sorted方法, 只能根據key進行排序 sorted(dit) Out[6]: [-2, -1, 1, 2]web 若是須要根據value排序,能夠 sorted(dit.items(),key = lambda x:x[1],reverse = True) Out[7]: [(-2, ‘d’), (2, ‘c’
相關文章
相關標籤/搜索