1. 程式人生 > >python dict 排序

python dict 排序

uil 排序 extra val tuples ini rst tool 效率

我們知道Python的內置dictionary數據類型是無序的,通過key來獲取對應的value。可是有時我們需要對dictionary中 的item進行排序輸出,可能根據key,也可能根據value來排。到底有多少種方法可以實現對dictionary的內容進行排序輸出呢?下面摘取了 一些精彩的解決辦法。

python對容器內數據的排序有兩種,一種是容器自己的sort函數,一種是內建的sorted函數。

sort函數和sorted函數唯一的不同是,sort是在容器內(in-place)排序,sorted生成一個新的排好序的容器。

對於一個簡單的數組 L=[5,2,3,1,4].

(1) L.sort(), sort

(comp=None, key=None, reverse=False) --> in place sort

(2) sorted(iterable, cmp=None, key=None, reverse=False) --> return a new sorted list

  • cmp specifies a custom comparison function of two arguments (iterable elements) which should return a negative, zero or positive number depending on whether the first argument is considered smaller than, equal to, or larger than the second argument: cmp=lambda x,y: cmp(x.lower(), y.lower())
    . The default value is None.
  • key specifies a function of one argument that is used to extract a comparison key from each list element: key=str.lower. The default value is None (compare the elements directly).
  • reverse is a boolean value. If set to True, then the list elements are sorted as if each comparison were reversed.

In general, the key and reverse conversion processes are much faster than specifying an equivalent cmp function. This is because cmp is called multiple times for each list element while key and reverse touch each element only once. Use functools.cmp_to_key() to convert an old-style cmp function to a key function.

The built-in sorted() function is guaranteed to be stable. A sort is stable if it guarantees not to change the relative order of elements that compare equal — this is helpful for sorting in multiple passes (for example, sort by department, then by salary grade).

算法的穩定性,基數排序正確性的保證,在低位排序好後,相同的高位排在一起,但要保證之前的地位相對順序不變。

1 按照Key值排序

#最簡單的方法,這個是按照key值排序: 
def sortedDictValues1(adict): 
items = adict.items() 
items.sort() 
return [value for key, value in items] 

#又一個按照key值排序,貌似比上一個速度要快點 
def sortedDictValues2(adict): 
keys = adict.keys() 
keys.sort() 
return [dict[key] for key in keys] 

#還是按key值排序,據說更快。。。而且當key為tuple的時候照樣適用 
def sortedDictValues3(adict): 
keys = adict.keys() 
keys.sort() 
return map(adict.get, keys) 

#一行語句搞定: 
[(k,di[k]) for k in sorted(di.keys())] 

#用sorted函數的key參數(func)排序: 
#按照key進行排序 
print sorted(dict1.items(), key=lambda d: d[0])

2 按照value值排序

#來一個根據value排序的,先把item的key和value交換位置放入一個list中,再根據list每個元素的第一個值,即原來的value值,排序: 
def sort_by_value(d): 
items=d.items() 
backitems=[[v[1],v[0]] for v in items] 
backitems.sort() 
return [ backitems[i][1] for i in range(0,len(backitems))] 

#還是一行搞定: 
[ v for v in sorted(di.values())] 

#用lambda表達式來排序,更靈活: 
sorted(d.items(), lambda x, y: cmp(x[1], y[1])), 或反序: 
sorted(d.items(), lambda x, y: cmp(x[1], y[1]), reverse=True) 

#用sorted函數的key參數(func)排序: # 按照value進行排序 
print sorted(dict1.items(), key=lambda d: d[1]) 

3 擴展用法:Key Function:

從Python2.4開始,list.sort() 和 sorted() 都增加了一個 ‘key’ 參數用來在進行比較之前指定每個列表元素上要調用的函數。

例1: 不區分大小寫的字符串比較排序:

>>> sorted("This is a test string from Andrew".split(), key=str.lower)
[a, Andrew, from, is, string, test, This]

key應該是一個函數,其接收一個參數,並且返回一個用於排序依據的key。其執行效率很高,因為對於輸入記錄key function能夠準確的被調用。

例2 對於復雜的對象,使用對象的下標作為key:

>>> student_tuples = [
...     (john, A, 15),
...     (jane, B, 12),
...     (dave, B, 10),
... ]
>>> sorted(student_tuples, key=lambda student: student[2])   # sort by age
[(dave, B, 10), (jane, B, 12), (john, A, 15)]

例3 使用對象的屬性進行操作:

>>> class Student:
...     def __init__(self, name, grade, age):
...         self.name = name
...         self.grade = grade
...         self.age = age
...     def __repr__(self):
...         return repr((self.name, self.grade, self.age))
>>>
>>> student_objects = [
...     Student(john, A, 15),
...     Student(jane, B, 12),
...     Student(dave, B, 10),
... ]
>>> sorted(student_objects, key=lambda student: student.age)   # sort by age
[(dave, B, 10), (jane, B, 12), (john, A, 15)]

當列表裏面每一個元素不止一個元素(比如:列表裏面,元素為元祖類型),我們除了想對第一個關鍵字排序之外,還想在第一次的基礎上面根據第二個關鍵字進行排序:

>>> list2 = [(d, 3), (a, 5), (d, 1), (c, 2), (d, 2)]
>>> list2
[(d, 3), (a, 5), (d, 1), (c, 2), (d, 2)]
>>> list2.sort()
>>> list2
[(a, 5), (c, 2), (d, 1), (d, 2), (d, 3)]
>>> list3 = [(d, 3), (a, 5), (d, 1), (c, 2), (d, 2)]
>>> sorted(list3, key = lambda x:(x[0],x[1]))
[(a, 5), (c, 2), (d, 1), (d, 2), (d, 3)]

參考:

python function: sorted

Python 排序---sort與sorted學習

python dict sorted 排序

Python: sort,sorted,OrderedDict的用法

python dict 排序