具體內(nèi)容如下:
使用sorted將字典按照其value大小排序
>>> record = {'a':89, 'b':86, 'c':99, 'd':100}>>> sorted(record.items(), key=lambda x:x[1])[('b', 86), ('a', 89), ('c', 99), ('d', 100)] sorted第一個(gè)參數(shù)要可迭代,可以為tuple, list
>>> items = [(1, 'B'), (1, 'A'), (2, 'A'), (0, 'B'), (0, 'a')]>>> sorted(items)[(0, 'B'), (0, 'a'), (1, 'A'), (1, 'B'), (2, 'A')]
為什么(0, 'B')在(0, 'a')前面?
因?yàn)锳SCII碼中大寫(xiě)字母排在在小寫(xiě)字母前面,使用str.lower()方法改變其順序
>>> sorted(items, key=lambda x:(x[0], x[1].lower()))
[(0, 'a'), (0, 'B'), (1, 'A'), (1, 'B'), (2, 'A')]
以上內(nèi)容是關(guān)于Python實(shí)現(xiàn)字典依據(jù)value排序的相關(guān)知識(shí),希望對(duì)大家有所幫助!



















