Python 一次排序多个字典维度
我有一本以下形式的词典:Python 一次排序多个字典维度,python,python-3.x,sorting,dictionary,Python,Python 3.x,Sorting,Dictionary,我有一本以下形式的词典: { "b":{ "ba": { "Score":3, "N":500 }, "aa": { "Score": 10, "N":300 }, }, "a":{ "xy": {
{
"b":{
"ba": {
"Score":3,
"N":500
},
"aa": {
"Score": 10,
"N":300
},
},
"a":{
"xy": {
"Score":199,
"N":7
},
"zz": {
"Score":222,
"N":55
},
}
}
我希望第一个维度按字母顺序排序,但第二个维度按分数排序,这样在排序时看起来像:
{
"a":{
"zz": {
"Score":222,
"N":55
},
"xy": {
"Score":199,
"N":7
}
},
"b":{
"aa": {
"Score": 10,
"N":300
},
"ba": {
"Score":3,
"N":500
}
}
}
我寻找这个问题的答案已经有一段时间了,但我只找到了如何在字典中对单个维度进行排序。做这件事最好的方法是什么
在排序之后,我需要将其转储到一个文件中,我正计划使用json
模块对其进行排序。所以做一些类似于:
with open("out.json", 'w') as fp:
json.dump(my_dict, fp, sort_keys=True, indent=4)
因为普通字典是无序的,所以将已排序的dict放入另一个普通dict会再次丢弃顺序
然后首先对外部键进行排序,然后对内部字典进行排序:
from collections import OrderedDict
d = {"b":{"ba": {"Score":3, "N":500},
"aa": {"Score": 10, "N":300},},
"a":{"xy": {"Score":199, "N":7},
"zz": {"Score":222, "N":55},}}
d = OrderedDict(sorted(d.items())) # sorts the outer keys
for k, v in d.items():
# sort the inner keys by score
d[k] = OrderedDict(sorted(v.items(), key=lambda x: x[1]['Score'], reverse=True))
之后的d
如下所示:
OrderedDict([('a',
OrderedDict([('zz', {'N': 55, 'Score': 222}),
('xy', {'N': 7, 'Score': 199})])),
('b',
OrderedDict([('aa', {'N': 300, 'Score': 10}),
('ba', {'N': 500, 'Score': 3})]))])
因为普通字典是无序的,所以将已排序的dict放入另一个普通dict会再次丢弃顺序
然后首先对外部键进行排序,然后对内部字典进行排序:
from collections import OrderedDict
d = {"b":{"ba": {"Score":3, "N":500},
"aa": {"Score": 10, "N":300},},
"a":{"xy": {"Score":199, "N":7},
"zz": {"Score":222, "N":55},}}
d = OrderedDict(sorted(d.items())) # sorts the outer keys
for k, v in d.items():
# sort the inner keys by score
d[k] = OrderedDict(sorted(v.items(), key=lambda x: x[1]['Score'], reverse=True))
之后的d
如下所示:
OrderedDict([('a',
OrderedDict([('zz', {'N': 55, 'Score': 222}),
('xy', {'N': 7, 'Score': 199})])),
('b',
OrderedDict([('aa', {'N': 300, 'Score': 10}),
('ba', {'N': 500, 'Score': 3})]))])
您可以使用以下方法创建适当的示例:
ordered = OrderedDict(
[(k, OrderedDict(
sorted(v.items(), key=lambda pair: -pair[1]["Score"])))
for k, v in sorted(data.items())])
现在将其转储到json
>>> print(json.dumps(ordered, indent=2))
{
"a": {
"zz": {
"Score": 222,
"N": 55
},
"xy": {
"Score": 199,
"N": 7
}
},
"b": {
"aa": {
"Score": 10,
"N": 300
},
"ba": {
"Score": 3,
"N": 500
}
}
}
注意:您可以在内部排序中使用reverse=True
,而不是否定分数,这取决于您的感受…您可以使用例如:
ordered = OrderedDict(
[(k, OrderedDict(
sorted(v.items(), key=lambda pair: -pair[1]["Score"])))
for k, v in sorted(data.items())])
现在将其转储到json
>>> print(json.dumps(ordered, indent=2))
{
"a": {
"zz": {
"Score": 222,
"N": 55
},
"xy": {
"Score": 199,
"N": 7
}
},
"b": {
"aa": {
"Score": 10,
"N": 300
},
"ba": {
"Score": 3,
"N": 500
}
}
}
注意:您可以在内部排序中使用reverse=True
而不是否定分数,这取决于您的感受…只是一个小障碍:字典是无序的。@mseifer我知道字典在Python中存储时是无序的,但我需要将其排序(如上所述)输出到json文件中,根据我的编辑。所以使用一个OrderedDict
就可以了?构建一个包含其他OrderedDict的OrderedDict并将其作为json转储。只是一个小障碍:字典是无序的。@mseifer我知道字典在Python中存储时是无序的,但我需要将其排序(如上所述)输出到json文件中,根据我的编辑。那么使用一个orderedict
就可以了?构建一个包含其他orderedict的orderedict并将其作为json转储。