使用列表创建嵌套字典

时间:2020-08-27 16:02:30

标签: python dictionary nested

我想创建一个很长的嵌套字典,但是有些而不是创建新项目,而是不断更新相同的最后一项。

sessions = ['S0_DO','S1_DO','S2_DO','S3_DO','S4_DO','S5_DO']
groups = ['All','Aggregator','Non-Aggregator']
comparator = {}


for session in sessions:
    for group in groups:
        c = {
            "time" : "2019-09-20 10:30:00",
            session :{
                group:{
                    "std":0,
                    "mean":0,
                    "upper_limit":0,
                    "lower_limit":0,
                    "actual":0,
                    "anomaly":0
                }
            }
        }
        comparator.update(c)

所有会话均已创建,但在分组方面,只有列表的最后一项在词典中。它只是更新而不是创建新的。

我该如何解决?

谢谢

所需输出:

comparator = {
    "time" : "2019-09-20 10:30:00",
    "S0_DO" :{
        "All":{
            "std":0,
            "mean":0,
            "upper_limit":0,
            "lower_limit":0,
            "actual":0,
            "anomaly":0
        },
        "Aggregator":{
            "std":0,
            "mean":0,
            "upper_limit":0,
            "lower_limit":0,
            "actual":0,
            "anomaly":0
            
        },
        "Non-Aggregator":{
            "std":0,
            "mean":0,
            "upper_limit":0,
            "lower_limit":0,
            "actual":0,
            "anomaly":0

        }
    },
        "S1_DO" :{
        "All":{
            "std":0,
            "mean":0,
            "upper_limit":0,
            "lower_limit":0,
            "actual":0,
            "anomaly":0
        },
        "Aggregator":{
            "std":0,
            "mean":0,
            "upper_limit":0,
            "lower_limit":0,
            "actual":0,
            "anomaly":0
            
        },
        "Non-Aggregator":{
            "std":0,
            "mean":0,
            "upper_limit":0,
            "lower_limit":0,
            "actual":0,
            "anomaly":0

        }
    },
        "S2_DO" :{
        "All":{
            "std":0,
            "mean":0,
            "upper_limit":0,
            "lower_limit":0,
            "actual":0,
            "anomaly":0
        },
        "Aggregator":{
            "std":0,
            "mean":0,
            "upper_limit":0,
            "lower_limit":0,
            "actual":0,
            "anomaly":0
            
        },
        "Non-Aggregator":{
            "std":0,
            "mean":0,
            "upper_limit":0,
            "lower_limit":0,
            "actual":0,
            "anomaly":0

        }
    },
        "S3_DO" :{
        "All":{
            "std":0,
            "mean":0,
            "upper_limit":0,
            "lower_limit":0,
            "actual":0,
            "anomaly":0
        },
        "Aggregator":{
            "std":0,
            "mean":0,
            "upper_limit":0,
            "lower_limit":0,
            "actual":0,
            "anomaly":0
            
        },
        "Non-Aggregator":{
            "std":0,
            "mean":0,
            "upper_limit":0,
            "lower_limit":0,
            "actual":0,
            "anomaly":0

        }
    },
        "S4_DO" :{
        "All":{
            "std":0,
            "mean":0,
            "upper_limit":0,
            "lower_limit":0,
            "actual":0,
            "anomaly":0
        },
        "Aggregator":{
            "std":0,
            "mean":0,
            "upper_limit":0,
            "lower_limit":0,
            "actual":0,
            "anomaly":0
            
        },
        "Non-Aggregator":{
            "std":0,
            "mean":0,
            "upper_limit":0,
            "lower_limit":0,
            "actual":0,
            "anomaly":0

        }
    },
        "S5_DO" :{
        "All":{
            "std":0,
            "mean":0,
            "upper_limit":0,
            "lower_limit":0,
            "actual":0,
            "anomaly":0
        },
        "Aggregator":{
            "std":0,
            "mean":0,
            "upper_limit":0,
            "lower_limit":0,
            "actual":0,
            "anomaly":0
            
        },
        "Non-Aggregator":{
            "std":0,
            "mean":0,
            "upper_limit":0,
            "lower_limit":0,
            "actual":0,
            "anomaly":0

        }
    }
}

1 个答案:

答案 0 :(得分:0)

group循环的每次迭代中,您都使用仅容纳该单个组的新字典覆盖了session键的值,因此所有会话仅以最后一个组结束。您可能想用字典理解替换内部循环。

comparator = {"time": "2019-09-20 10:30:00"}
for session in sessions:
    c = {
        session: {
            group: {
                "std": 0,
                "mean": 0,
                "upper_limit": 0,
                "lower_limit": 0,
                "actual": 0,
                "anomaly": 0,
            }
            for group in groups
        }
    }
    comparator.update(c)
相关问题