Python字典按鍵排序的多種方法


排序是指按定義的順序排列元素的技術。在Python中,有多種方法可以按鍵對字典進行排序。本文將學習這些方法。

使用sorted()函式

sorted() 函式用於對元組、字典、列表等可迭代資料結構中的元素進行排序。此函式將可迭代物件作為引數,並返回一個新的已排序列表,其中包含已排序的元素作為輸出。以下是將sorted()函式用於字典的語法。

sorted(dictionary.keys())

其中:

  • sorted 是用於按鍵排序字典的函式。

  • dictionary 是輸入字典。

  • keys 是獲取字典鍵的函式。

示例

在此示例中,我們將字典鍵作為輸入引數傳遞給sorted函式,然後將返回字典的已排序元素作為輸出。

dic = {"Name" : ["Karthikeya","Anil","Srivatsav","Riyaansh","Prasad"],
      "Age":[1,30,25,1,55],
      "Skill":["python","Salesforce","Networking","python","Management"]}
print("The dictionary before sorting:",dic)
sorted_dic = sorted(dic.keys())
print("The dictionary after sorting:",sorted_dic)

輸出

執行以上程式碼後,將顯示以下輸出:

The dictionary before sorting: {'Name': ['Karthikeya', 'Anil', 'Srivatsav', 'Riyaansh', 'Prasad'], 'Age': [1, 30, 25, 1, 55], 'Skill': ['python', 'Salesforce', 'Networking', 'python', 'Management']}
The dictionary after sorting: ['Age', 'Name', 'Skill']

使用sorted()函式的“key”引數

Python中sorted()函式的key引數用於根據字典的特定屬性或特性指定自定義排序順序。以下是使用sorted()函式的key引數按鍵排序字典的語法。

sorted(dic, key = function)

示例

在此示例中,我們將使用一個函式來獲取指定的鍵,並將其定義為sorted函式的key引數,然後將根據該鍵進行排序。

dic = [{"Name" : ["Karthikeya","Anil","Srivatsav","Riyaansh","Prasad"],
      "Age":[1,30,25,1,55],
      "Skill":["python","Salesforce","Networking","python","Management"]},
       {"Name": ["Krishna","kumari","Madhuri","Gayathri"],
        "Age" : [0,50,27,28]}]
print("The dictionary before sorting:",dic)
sorted_dic = sorted(dic, key = lambda x : x['Age'])
print("The dictionary after sorting:",sorted_dic)

輸出

使用sorted函式的key引數對字典按鍵排序的輸出。

The dictionary before sorting: [{'Name': ['Karthikeya', 'Anil', 'Srivatsav', 'Riyaansh', 'Prasad'], 'Age': [1, 30, 25, 1, 55], 'Skill': ['python', 'Salesforce', 'Networking', 'python', 'Management']}, {'Name': ['Krishna', 'kumari', 'Madhuri', 'Gayathri'], 'Age': [0, 50, 27, 28]}]
The dictionary after sorting: [{'Name': ['Krishna', 'kumari', 'Madhuri', 'Gayathri'], 'Age': [0, 50, 27, 28]}, {'Name': ['Karthikeya', 'Anil', 'Srivatsav', 'Riyaansh', 'Prasad'], 'Age': [1, 30, 25, 1, 55], 'Skill': ['python', 'Salesforce', 'Networking', 'python', 'Management']}]

使用collections.OrderedDict()

在Python中,collections模組提供了一個OrderedDict類,用於維護鍵的插入順序,此類可用於按鍵對字典進行排序。以下是使用collections.OrderedDict()的語法。

collections.OrderedDict(sorted(dictionary.items()))

示例

在此示例中,我們將使用collections.OrderedDict()按鍵對字典進行排序,然後輸出將是按鍵排序的字典。

import collections
dic ={"Name":["Karthikeya","Anil","Srivatsav","Riyaansh","Prasad"],
"Age":[1,30,25,1,55],
"Skill":["python","Salesforce","Networking","python","Management"]}  
print("The dictionary before sorting:",dic)
sorted_dic = collections.OrderedDict(sorted(dic.items()))
print("The dictionary after sorting:",sorted_dic)

輸出

以下是使用collections.OrderedDict()函式按鍵對字典排序的輸出。

The dictionary before sorting: {'Name': ['Karthikeya', 'Anil', 'Srivatsav', 'Riyaansh', 'Prasad'], 'Age': [1, 30, 25, 1, 55], 'Skill': ['python', 'Salesforce', 'Networking', 'python', 'Management']}
The dictionary after sorting: OrderedDict([('Age', [1, 30, 25, 1, 55]), ('Name', ['Karthikeya', 'Anil', 'Srivatsav', 'Riyaansh', 'Prasad']), ('Skill', ['python', 'Salesforce', 'Networking', 'python', 'Management'])])

使用列表推導式

列表推導式用於使用sorted()函式按鍵對字典進行排序。以下是使用列表推導式的語法。

key for key in sorted(dictionary.keys())

示例

在此示例中,我們將使用列表推導式按鍵對字典進行排序。結果將是按鍵排序的字典。

import collections
dic = {"Name" : ["Karthikeya","Anil","Srivatsav","Riyaansh","Prasad"],
      "Age":[1,30,25,1,55],
      "Skill":["python","Salesforce","Networking","python","Management"]}  
print("The dictionary before sorting:",dic)
sorted_keys = [key for key in sorted(dic.keys())]
print("The dictionary after sorting:",sorted_keys)
for key in sorted_keys:
    print(key,":",dic[key])	

輸出

以下是使用列表推導式按鍵對字典排序的輸出。

The dictionary before sorting: {'Name': ['Karthikeya', 'Anil', 'Srivatsav', 'Riyaansh', 'Prasad'], 'Age': [1, 30, 25, 1, 55], 'Skill': ['python', 'Salesforce', 'Networking', 'python', 'Management']}
The dictionary after sorting: ['Age', 'Name', 'Skill']
Age : [1, 30, 25, 1, 55]
Name : ['Karthikeya', 'Anil', 'Srivatsav', 'Riyaansh', 'Prasad']
Skill : ['python', 'Salesforce', 'Networking', 'python', 'Management']

更新於:2023年10月20日

瀏覽量:125

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告