嵌套字典按值排序

时间:2014-05-09 08:36:30

标签: python sorting dictionary tree categories

我有一个嵌套的dict,用作类别树。

我想按树的每个分支中的权重值对字典进行排序。

我搜索过,但是我找不到解决办法。

提前谢谢

注意:我最小化了dict的易读性。

cat={"categories": 
    {
        "1": {
            "child": {
                "10": {
                    "child": {
                        "12": {
                            "child": {}, 
                            "id": 12, 
                            "weight": 374.0
                        }, 
                        "156": {
                            "child": {}, 
                            "id": 156, 
                            "weight": 93.0
                        }, 
                        "279": {
                            "child": {}, 
                            "id": 279, 
                            "weight": 137.0
                        }, 
                        "280": {
                            "child": {}, 
                            "id": 280, 
                            "weight": 340.0
                        }
                    }, 
                    "id": 10, 
                    "weight": 574.0
                }, 
                "13": {
                    "child": {
                        "14": {
                            "child": {}, 
                            "id": 14, 
                            "weight": 3519.0
                        }, 
                        "15": {
                            "child": {}, 
                            "id": 15, 
                            "weight": 4327.0
                        }, 
                        "264": {
                            "child": {}, 
                            "id": 264, 
                            "weight": 108.0
                        }
                    }, 
                    "id": 13, 
                    "weight": 3646.0
                }
            }, 
            "id": 1, 
            "weight": 8695.0
        }, 
        "6": {
            "child": {}, 
            "id": 6, 
            "weight": 24584.0
        }
    }
}

编辑:

预期收益(按重量排序):

cat={"categories": 
    {
        "6": {
            "child": {}, 
            "id": 6, 
            "weight": 24584.0
        },
        "1": {
            "child": {
                "13": {
                    "child": {
                        "15": {
                            "child": {}, 
                            "id": 15, 
                            "weight": 4327.0
                        }, 
                        "14": {
                            "child": {}, 
                            "id": 14, 
                            "weight": 3519.0
                        }, 
                        "264": {
                            "child": {}, 
                            "id": 264, 
                            "weight": 108.0
                        }
                    }, 
                    "id": 13, 
                    "weight": 3646.0
                },
                "10": {
                    "child": {
                        "12": {
                            "child": {}, 
                            "id": 12, 
                            "weight": 374.0
                        }, 
                        "280": {
                            "child": {}, 
                            "id": 280, 
                            "weight": 340.0
                        },
                        "279": {
                            "child": {}, 
                            "id": 279, 
                            "weight": 137.0
                        }, 
                        "156": {
                            "child": {}, 
                            "id": 156, 
                            "weight": 93.0
                        }
                    }, 
                    "id": 10, 
                    "weight": 574.0
                }
            }, 
            "id": 1, 
            "weight": 8695.0
        }
    }
}

0 个答案:

没有答案