用Python排序字典

时间:2020-02-23 14:43:45  来源:igfitidea点击:

介绍

在本教程中,我们将研究在Python中对字典进行排序的各种方法。

在Python中,字典是键值对的集合。
这样的键和值对的顺序以逗号分隔。
这些对称为项目。

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

如上所述,字典项由键及其对应的值组成。
因此,可以通过使用键或者值部分中的任何一个作为参数来对字典进行排序。

因此,现在让我们看一下可以通过键或者值对字典进行排序的不同方法。

1.按关键字排序字典

我们可以使用Python中内置的sorted()方法直接对字典进行排序。
这可以通过传递字典本身和一个函数来完成,该函数指定要基于其进行排序的参数(在本例中为key)。

让我们看看如何。

d = { 5: 1 , 4: 2 , 3: 3 , 2: 4 , 1: 5 }

print("The original dictionary: ", d)

#sorting by key
a = dict(sorted(d.items(), key=lambda x: x[0]))

print("After sorting by key: ", a)

输出:

The original dictionary:  {5: 1, 4: 2, 3: 3, 2: 4, 1: 5}
After sorting by key:  {1: 5, 2: 4, 3: 3, 4: 2, 5: 1}

其中

  • d.items()方法返回一个包含键及其对应值的元组(items)列表,
  • lambda函数返回特定项目元组的键(第0个元素),
  • 当这些被传递给sorted()方法时,它返回一个排序的序列,然后将其类型转换为字典。

请记住,此方法可在Python 3.6+版本中使用,因为它将字典视为有序序列。
对于较旧的版本,我们可以使用operator模块中的itemgetter()方法替换lambda函数。
让我们看看如何。

from operator import itemgetter

d = { 5: 1 , 4: 2 , 3: 3 , 2: 4 , 1: 5 }
print("Original Dict: ", d)

#sorting by key
s_d = dict(sorted(d.items(), key=itemgetter(0)))
print("Sorted dict: ", s_d)

输出:

Original Dict:  {5: 1, 4: 2, 3: 3, 2: 4, 1: 5}
Sorted dict:  {1: 5, 2: 4, 3: 3, 4: 2, 5: 1}

这里的sorted()和items()方法也以相同的方式工作。
但是代替lambda函数,itemgetter(0)方法返回一个可调用对象,该对象使用操作数的__getitem __()方法从其操作数中获取第0个项目。
在这种情况下,由于需要按键排序,因此我们考虑第0个元素。

2.按值对字典排序

按值对字典进行排序类似于按键对字典进行排序。
唯一的区别在于,此类型的参数将根据其进行排序的是相应项目的值部分。

因此,正如我们之前所做的那样,对于Python 3.6及更高版本,我们可以将sorted()方法与lambda函数一起使用。
让我们看看如何。

d = { 0: 'd', 1: 'c', 2: 'b', 3: 'a' }

print("The original dictionary: ", d)

#sorting by value
a = dict(sorted(d.items(), key=lambda x: x[1]) )

print("After sorting by value: ", a)

输出:

The original dictionary:  {0: 'd', 1: 'c', 2: 'b', 3: 'a'}
After sorting by value:  {3: 'a', 2: 'b', 1: 'c', 0: 'd'}

同样其中根据lambda函数返回的值(项x的x [1]值)对字典d进行排序。

同样,对于旧版本的Python,请遵循以下方法。

from operator import itemgetter

d = { 0: 'd', 1: 'c', 2: 'b' , 3: 'a' }
print("Original Dict: ", d)

#sorting by value
s_d = dict(sorted(d.items(), key=itemgetter(1)))
print("Sorted dict: ", s_d)

输出:

Original Dict:  {0: 'd', 1: 'c', 2: 'b', 3: 'a'}
Sorted dict:  {3: 'a', 2: 'b', 1: 'c', 0: 'd'}

类似地,sorted()连同d.items()和itemgetter(1)方法根据值对字典d进行排序。

3.反向排序

sorted()方法带有另一个参数reverse
可以用于指定排序的顺序。
如果通过" True",则排序以相反的顺序(降序)进行。
并且如果传递了False(默认),则排序以升序进行。

让我们尝试通过一个示例来尝试理解这一点,在该示例中,我们尝试按键对字典进行反向排序。

d = { 'a': 23, 'g': 67, 'e': 12, 45: 90}

print("The original dictionary: ", d)

#sorting by value in reverse
a = dict(sorted(d.items(), reverse = True, key=lambda x: x[1]))
print("After sorting by value in reverse order: ", a)

#sorting by value in ascending order
a = dict(sorted(d.items(), key=lambda x: x[1]))#by default reverse is set to False
print("After sorting by value in ascending order: ", a)

输出:

The original dictionary:  {'a': 23, 'g': 67, 'e': 12, 45: 90}
After sorting by value in reverse order:  {45: 90, 'g': 67, 'a': 23, 'e': 12}
After sorting by value in ascending order:  {'e': 12, 'a': 23, 'g': 67, 45: 90}

从上面的输出中可以清楚地看出,将反向字典作为True传递给上面的字典是按反向顺序(降序)排序的。