技术文章和资源

技术文章(时间排序)

热门类别

Python PHP MySQL JDBC Linux

按键对 Python 字典进行排序的不同方法

pythonserver side programmingprogramming

排序是指按定义的顺序排列元素的技术。在 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)

输出

当我们运行上述代码时,将显示以下输出 -

排序前的字典:{'Name': ['Karthikeya', 'Anil', 'Srivatsav', 'Riyaansh', 'Prasad'], 'Age': [1, 30, 25, 1, 55], 'Skill': ['python', 'Salesforce', 'Networking', 'python', 'Management']}
排序后的字典:['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']

相关文章