简体   繁体   English

在Python 3中对字典列表进行排序

[英]Sorting a list of dictionaries in Python 3

I am trying to sort a list of dictionaries. 我正在尝试对词典列表进行排序。 My goal is to sort dictionaries with multiple (possibly the same) keys in the same way, even if the dictionaries are in a different order or if the keys are in the dictionary in a different order. 我的目标是以相同的方式对具有多个(可能是相同的)键的字典进行排序,即使字典的顺序不同或字典中的键的顺序也不同。

In Python 2, I have used the following: 在Python 2中,我使用了以下内容:

a = [{1: 2, 7: 8}, {7: 8, 3: 4}, {5: 6}]
b = [{3: 4, 7: 8}, {7: 8, 1: 2}, {5: 6}]
a.sort()
b.sort()
a
Out[20]: [{5: 6}, {1: 2, 7: 8}, {3: 4, 7: 8}]
b
Out[21]: [{5: 6}, {1: 2, 7: 8}, {3: 4, 7: 8}]

This succeeds in my goal of creating two sorted dictionaries that look exactly the same. 这成功实现了我创建两个看起来完全相同的排序字典的目标。

I am trying to do the same thing in Python 3, where .sort() does not work for a list of dictionaries. 我正在尝试在Python 3中做同样的事情,其中​​.sort()对于字典列表不起作用。

I have tried different ways. 我尝试了不同的方法。

1. 1。

sorted(a, key=lambda d: max(d.keys()))

This does not work: 这不起作用:

a = [{1: 2, 7: 8}, {3: 4, 7: 8}, {5: 6}]
b = [{3: 4, 7: 8}, {1: 2, 7: 8}, {5: 6}]
a2 = sorted(a, key=lambda d: max(d.keys()))
b2 = sorted(b, key=lambda d: max(d.keys()))
a2
Out[1]: [{5: 6}, {1: 2, 7: 8}, {7: 8, 3: 4}]
b2
Out[2]: [{5: 6}, {3: 4, 7: 8}, {7: 8, 1: 2}]

2. 2。

a2 = sorted([list(zip(x.keys(),x.values())) for x in a])
a3 = [{k: v for (k,v) in x} for x in a2]

This does not work: 这不起作用:

a = [{1: 2, 7: 8}, {7: 8, 3: 4}, {5: 6}]
b = [{3: 4, 7: 8}, {7: 8, 1: 2}, {5: 6}]
a2 = sorted([list(zip(x.keys(),x.values())) for x in a])
a3 = [{k: v for (k,v) in x} for x in a2]
b2 = sorted([list(zip(x.keys(),x.values())) for x in b])
b3 = [{k: v for (k,v) in x} for x in b2]
a3
Out[1]: [{1: 2, 7: 8}, {5: 6}, {7: 8, 3: 4}]
b3
Out[2]: [{3: 4, 7: 8}, {5: 6}, {7: 8, 1: 2}]

Does anyone have an idea how I can get the Python 2 result in Python 3?? 有谁知道如何在Python 3中获得Python 2结果?

Sorting on all keys in the dictionaries can be done with: 可以对字典中的所有keys进行排序:

a.sort(key=lambda d: d.keys())
b.sort(key=lambda d: d.keys())

To get the result you want we need to sort the keys on ascending order as follows: 为了获得想要的结果,我们需要按以下顺序对键进行升序排序:

a.sort(key=lambda d: sorted(list(d.keys()), reverse=True))
b.sort(key=lambda d: sorted(list(d.keys()), reverse=True))

This gives the following result: 得到以下结果:

>>> a
[{5: 6}, {1: 2, 7: 8}, {3: 4, 7: 8}]
>>> b
[{5: 6}, {1: 2, 7: 8}, {3: 4, 7: 8}]

Edit: In order to sort based on the values as well (asked in the comments) the following might work: 编辑:为了也基于值进行排序(在注释中要求),可以使用以下方法:

a.sort(key=lambda d: sorted(list(d.keys()) + sorted(list(d.values())), reverse=True))
b.sort(key=lambda d: sorted(list(d.keys()) + sorted(list(d.values())), reverse=True))

You need to use all items of each dict while sorting, and d.items() iterates items in arbitrary order, so they need to be sorted. 您需要在排序时使用每个字典的所有项目,并且d.items()以任意顺序迭代项目,因此需要对它们进行排序。 And this is your sorting key: 这是您的排序键:

a = [{1: 2, 7: 8}, {7: 8, 3: 4}, {5: 6}]
b = [{3: 4, 7: 8}, {7: 8, 1: 2}, {5: 6}]
sorted(a, key=lambda d: sorted(d.items()))
Out: [{1: 2, 7: 8}, {3: 4, 7: 8}, {5: 6}]

sorted(b, key=lambda d: sorted(d.items()))
Out: [{1: 2, 7: 8}, {3: 4, 7: 8}, {5: 6}]

x = [{1: 2},{1: 3}]
y = [{1: 3},{1: 2}]
sorted(x, key=lambda d: sorted(d.items()))
Out: [{1: 2}, {1: 3}]

sorted(y, key=lambda d: sorted(d.items()))
Out: [{1: 2}, {1: 3}]

Note that sorted(a, key=...) creates a new list, while a.sort(key=...) makes sorting in place. 请注意, sorted(a, key=...)创建一个新列表,而a.sort(key=...)进行适当的排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM