📜  Python – 对元组字典进行排序

📅  最后修改于: 2022-05-13 01:54:54.413000             🧑  作者: Mango

Python – 对元组字典进行排序

在本文中,我们将对元组字典进行排序。元组字典意味着元组是字典中的值或元组是字典中的键。

示例

{'key1': (1, 2, 3), 'key2': (3, 2, 1),.............}
or
{ (1, 2, 3):value, (3, 2, 1):value,.............}

方法一:使用 sorted() 方法

使用这种方法,我们可以根据键、值和项对元组字典进行排序,我们可以使用 for 循环对元组字典中的所有元素进行排序。

语法

To sort based on items:
for i in sorted(dictionary.items()) :
     print(i, end = " ")

To sort based on keys:
for i in sorted(dictionary.keys()) :
     print(i, end = " ") 
     
To sort based on values:
for i in sorted(dictionary.values()) :
     print(i, end = " ")           

示例 1:

Python3
# declare a dictionary of tuple with student data
data = {'student1': ('bhanu', 10), 'student4': ('uma', 12),
        'student3': ('suma', 11), 'student2': ('ravi', 11),
        'student5': ('gayatri', 9)}
  
# sort student dictionary based  on items
for i in sorted(data.items()):
    print(i, end=" ")
print()
  
# sort student dictionary based  on values
for i in sorted(data.values()):
    print(i, end=" ")
print()
  
# sort student dictionary based  on keys
for i in sorted(data.keys()):
    print(i, end=" ")


Python3
# declare a dictionary of tuple with student data
data = {('bhanu', 10): 'student1',
        ('uma', 12): 'student4', 
        ('suma', 11): 'student3', 
        ('ravi', 11): 'student2',
        ('gayatri', 9): 'student5'}
  
# sort student dictionary based  on items
for i in sorted(data.items()):
    print(i, end=" ")
print()
  
# sort student dictionary based  on values
for i in sorted(data.values()):
    print(i, end=" ")
print()
  
# sort student dictionary based  on keys
for i in sorted(data.keys()):
    print(i, end=" ")


Python3
# import oOrderedDict module
from collections import OrderedDict
  
# declare a dictionary of tuple with student data
data = {'student3': ('bhanu', 10), 'student2': ('uma', 12),
        'student4': ('sai', 11), 'student1': ('suma', 11)}
  
# sort student dictionary of tuple based
# on values using OrderedDict
print(OrderedDict(sorted(data.values())))
print()
  
# sort student dictionary of tuple based
# on items using OrderedDict
print(OrderedDict(sorted(data.items())))


Python3
# import orderedDict module
from collections import OrderedDict
  
# declare a dictionary of tuple with student data
data = {('bhanu', 10): 'student1', 
        ('uma', 12): 'student4', 
        ('suma', 11): 'student3', 
        ('ravi', 11): 'student2',
        ('gayatri', 9): 'student5'}
  
# sort student dictionary of tuple based 
# on items using OrderedDict
print(OrderedDict(sorted(data.items())))


输出:

我们也可以用键作为元组来制作一个元组字典。

示例 2:

Python程序以元组为键创建元组字典并应用 sorted()函数

Python3

# declare a dictionary of tuple with student data
data = {('bhanu', 10): 'student1',
        ('uma', 12): 'student4', 
        ('suma', 11): 'student3', 
        ('ravi', 11): 'student2',
        ('gayatri', 9): 'student5'}
  
# sort student dictionary based  on items
for i in sorted(data.items()):
    print(i, end=" ")
print()
  
# sort student dictionary based  on values
for i in sorted(data.values()):
    print(i, end=" ")
print()
  
# sort student dictionary based  on keys
for i in sorted(data.keys()):
    print(i, end=" ")

输出:

方法 2:使用 OrderedDict

OrderedDict 在用于使用 sorted() 方法对字典进行排序的集合模块中可用。

语法

To sort based on values:
OrderedDict(sorted(dictionary.values())

To sort based on items:
OrderedDict(sorted(dictionary.items())

示例 1:对元组字典进行排序的Python程序

Python3

# import oOrderedDict module
from collections import OrderedDict
  
# declare a dictionary of tuple with student data
data = {'student3': ('bhanu', 10), 'student2': ('uma', 12),
        'student4': ('sai', 11), 'student1': ('suma', 11)}
  
# sort student dictionary of tuple based
# on values using OrderedDict
print(OrderedDict(sorted(data.values())))
print()
  
# sort student dictionary of tuple based
# on items using OrderedDict
print(OrderedDict(sorted(data.items())))

输出:

示例 2:对元组字典进行排序的Python程序,其中元组将是字典中的键

Python3

# import orderedDict module
from collections import OrderedDict
  
# declare a dictionary of tuple with student data
data = {('bhanu', 10): 'student1', 
        ('uma', 12): 'student4', 
        ('suma', 11): 'student3', 
        ('ravi', 11): 'student2',
        ('gayatri', 9): 'student5'}
  
# sort student dictionary of tuple based 
# on items using OrderedDict
print(OrderedDict(sorted(data.items())))

输出: