如何在Python中根據字典的值對字典列表進行排序?
在本文中,我們將展示如何在Python中根據字典的值對字典列表進行排序。
排序一直是日常程式設計中一項有用的技術。Python的字典經常用於各種各樣的應用程式,從競賽到面向開發人員的應用程式(例如處理JSON資料)。在某些情況下,能夠根據字典的值過濾字典會很有用。
以下是完成此任務的兩種方法:
使用sorted()和itemgetter
使用sorted()和lambda函式
什麼是字典?
字典是Python版本的關聯陣列資料結構。字典是鍵值對的集合。每個鍵值對由一個鍵和與其關聯的值表示。
字典由用花括號括起來並用逗號分隔的鍵值對列表定義。每個鍵的值由冒號(:)分隔。
字典是一個有序的資料值集合,可以使用鍵訪問。字典是鍵值對映。`dict()`是一個用於建立dict類例項的建構函式。字典中的元素順序是未定義的。可以迭代鍵、值或鍵值對。
使用sorted()和itemgetter
sorted()函式
sorted()函式返回給定可迭代物件的已排序列表。
您可以選擇升序或降序。數字按數字排序,而字串按字母順序排列。
語法
sorted(iterable, key=key, reverse=reverse)
引數
iterable - 它是一個序列。
key - 將執行以確定順序的函式。預設值為None。
reverse - 布林表示式。True表示升序排序,False表示降序排序。預設值為False。
Python中的itemgetter
為了實現類似的功能,可以使用Itemgetter函式代替lambda函式。與sorted()和lambda相同,但內部實現不同。它接受字典鍵並將它們轉換為元組。它最大限度地減少了開銷,同時也更快、更高效。
要使用itemgetter,必須匯入"operator"模組。
效能 - 就效能而言,在時間方面,itemgetter優於lambda函式。
簡潔性 - 在訪問多個值時,itemgetter看起來比lambda函式更簡潔。
演算法(步驟)
以下是執行所需任務的演算法/步驟:
使用import關鍵字從operator模組匯入itemgetter以實現itemgetter。
建立一個變數來儲存輸入的字典列表。
使用sorted()函式(返回給定可迭代物件的已排序列表)和itemgetter列印按分數排序的列表。
示例
以下程式使用sorted()和itemgetter返回按值排序的輸入字典列表:
# importing itemgetter from the operator module # (for implementing itemgetter) from operator import itemgetter # Input list of dictionaries inputList = [{"cricketer": "Dhoni", "score": 75},{"cricketer": "Virat Kohli", "score": 90}, {"cricketer": "Hardik Pandya", "score": 65}] print("The sorted list by score: ") # printing the sorted list by score using the sorted() and itemgetter print(sorted(inputList, key=itemgetter('score'))) print("\r") # printing the sorted list by both cricketer, score using the sorted() and itemgetter # Here Dhoni comes first since 'D' comes first then H, and V alphabetically print("The sorted list by both cricketer and score: ") print(sorted(inputList, key=itemgetter('cricketer', 'score'))) print("\r") # printing the sorted list by the score in descending order # using the sorted() and itemgetter print("The sorted list by the score in descending order: ") print(sorted(inputList, key=itemgetter('score'), reverse=True))
輸出
執行上述程式將生成以下輸出:
The sorted list by score: [{'cricketer': 'Hardik Pandya', 'score': 65}, {'cricketer': 'Dhoni', 'score': 75}, {'cricketer': 'Virat Kohli', 'score': 90}] The sorted list by both cricketer and score: [{'cricketer': 'Dhoni', 'score': 75}, {'cricketer': 'Hardik Pandya', 'score': 65}, {'cricketer': 'Virat Kohli', 'score': 90}] The sorted list by the score in descending order: [{'cricketer': 'Virat Kohli', 'score': 90}, {'cricketer': 'Dhoni', 'score': 75}, {'cricketer': 'Hardik Pandya', 'score': 65}]
使用sorted()和lambda函式
示例
以下程式使用sorted()和lambda()函式返回按值排序的輸入字典列表:
# Input list of dictionaries inputList = [{"cricketer": "Dhoni", "score": 75},{"cricketer": "Virat Kohli", "score": 90}, {"cricketer": "Hardik Pandya", "score": 65}] print("The sorted list by score: ") # printing the sorted list by score using the sorted() and lambda functions print(sorted(inputList, key=lambda k: k['score'])) print("\r") # printing the sorted list by both cricketer, and score using the sorted() and lambda functions # Here Dhoni comes first since 'D' comes first then H, and V alphabetically print("The sorted list by both cricketer and score: ") print(sorted(inputList, key=lambda k: (k['cricketer'], k['score']))) print("\r") # printing the sorted list by the score in descending order # using the sorted() and lambda functions print("The sorted list by the score in descending order: ") print(sorted(inputList, key=lambda k: k['score'], reverse=True))
輸出
執行上述程式將生成以下輸出:
The sorted list by score: [{'cricketer': 'Hardik Pandya', 'score': 65}, {'cricketer': 'Dhoni', 'score': 75}, {'cricketer': 'Virat Kohli', 'score': 90}] The sorted list by both cricketer and score: [{'cricketer': 'Dhoni', 'score': 75}, {'cricketer': 'Hardik Pandya', 'score': 65}, {'cricketer': 'Virat Kohli', 'score': 90}] The sorted list by the score in descending order: [{'cricketer': 'Virat Kohli', 'score': 90}, {'cricketer': 'Dhoni', 'score': 75}, {'cricketer': 'Hardik Pandya', 'score': 65}]
結論
在這篇文章中,我們學習瞭如何透過兩種不同的方法根據值對字典列表進行排序。在本文中,我們還學習了lambda函式和itemgetter。