简体   繁体   English

如何以嵌套的 dict 格式对值进行排序?

[英]How do I sort values in a nested dict-format?

I currently have a dictionary with key: string , and values is another dict .我目前有一个带有 key: string的字典,而 values 是另一个dict

How do I sort by the values in the inner dictionary?如何按内部字典中的值排序? (I want to sort it by the number, 14, 8, 2, etc) (我想按数字排序,14、8、2等)

Example dictionary data :示例字典data

{
    "00": {
        "057fa729df76467eac31febae7c3d0587ab1db327eb4c9532ebbd63b3273df4d.json": 14,
        "067d2aaa8a300407c4b54f67c1fbb6c96aa3fe5ac8b71877e32636e5c0c5efe8.json": 2,
        "0d18dc8d9bf832afd01788bae9922d66a77acd7aa2ac07a1b292cb59c440345e.json": 8,

    },
    "000": {
        "02793d53bc9ed92d33207b0bbd03a63fffdf091ba98b2a1607242551ec850c88.json": 1,
        "02a606bed4864c58a78b1f1f60a7a8e6f297975c35ac889aaf717b205918cf57.json": 1,
        "03a735ca0ba08089106f1601ca391c34cdfbe0ec9b266fd66cc46f37c8f554de.json": 2,
    }
}

Expected Output:预期 Output:

After sorting by the number, it prints it from largest number to least (format does not have to be like this, but you get the point):按数字排序后,它从最大到最小打印它(格式不必是这样的,但你明白了):

    "057fa729df76467eac31febae7c3d0587ab1db327eb4c9532ebbd63b3273df4d.json": 14,
    "0d18dc8d9bf832afd01788bae9922d66a77acd7aa2ac07a1b292cb59c440345e.json": 8,
    "067d2aaa8a300407c4b54f67c1fbb6c96aa3fe5ac8b71877e32636e5c0c5efe8.json": 2,

My attempt:我的尝试:

When the key matches "00" it will print out all the values in that key: value pair.当键匹配“00”时,它将打印出该键中的所有值:值对。

for key, value in data.items()
    for k, v in sorted(value.items(), lambda x: x[1]):
        if key == "00":
           print(key)

Like this:像这样:

In [298]: my_dict = {}

In [299]: for key,value in data.items(): 
     ...:     my_dict[key] = {k: v for k, v in sorted(value.items(), key=lambda item: item[1], reverse=True)} 
     ...:                                                                                                                                                                                                   

In [300]: my_dict                                                                                                                                                                                           
Out[300]: 
{'00': {'057fa729df76467eac31febae7c3d0587ab1db327eb4c9532ebbd63b3273df4d.json': 14,
  '0d18dc8d9bf832afd01788bae9922d66a77acd7aa2ac07a1b292cb59c440345e.json': 8,
  '067d2aaa8a300407c4b54f67c1fbb6c96aa3fe5ac8b71877e32636e5c0c5efe8.json': 2},
 '000': {'03a735ca0ba08089106f1601ca391c34cdfbe0ec9b266fd66cc46f37c8f554de.json': 2,
  '02793d53bc9ed92d33207b0bbd03a63fffdf091ba98b2a1607242551ec850c88.json': 1,
  '02a606bed4864c58a78b1f1f60a7a8e6f297975c35ac889aaf717b205918cf57.json': 1}}

OR, you can use a nested dict comprehension :或者,您可以使用nested dict comprehension

In [305]: {key: {k: v for k, v in sorted(value.items(), key=lambda item: item[1], reverse=True)} for key,value in data.items()}                                                                             
Out[305]: 
{'00': {'057fa729df76467eac31febae7c3d0587ab1db327eb4c9532ebbd63b3273df4d.json': 14,
  '0d18dc8d9bf832afd01788bae9922d66a77acd7aa2ac07a1b292cb59c440345e.json': 8,
  '067d2aaa8a300407c4b54f67c1fbb6c96aa3fe5ac8b71877e32636e5c0c5efe8.json': 2},
 '000': {'03a735ca0ba08089106f1601ca391c34cdfbe0ec9b266fd66cc46f37c8f554de.json': 2,
  '02793d53bc9ed92d33207b0bbd03a63fffdf091ba98b2a1607242551ec850c88.json': 1,
  '02a606bed4864c58a78b1f1f60a7a8e6f297975c35ac889aaf717b205918cf57.json': 1}}

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

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