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']
广告