繁体   English   中英

使用列表创建嵌套字典

[英]create nested dictionary using list

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

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

        }
    }
}

group循环的每次迭代中,您使用仅包含该单个 group 的新 dict 覆盖session key 的 value,因此所有 session 都以最后一个 group 结束。 您可能想用字典理解替换内部循环。

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)

暂无
暂无

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

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