简体   繁体   English

使用列表创建嵌套字典

[英]create nested dictionary using list

I would like to create a very long nested dictionay, but some instead of creatin new items, it just keep updating the same last item.我想创建一个很长的嵌套字典,但有些不是创建新项目,而是不断更新最后一个项目。

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)

all the sessions are created, but when it comes to groups, only the last item of the list is in the dictionary.所有会话都已创建,但是当涉及到组时,字典中只有列表的最后一项。 it just updated instead of creating new one.它只是更新而不是创建新的。

How can I fix this ?我怎样才能解决这个问题 ?

Thanks谢谢

output desired :所需的输出:

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

        }
    }
}

In each iteration of the group loop, you overwrite the value of the session key with a new dict holding only that single group, thus all sessions end up with just the last group.group循环的每次迭代中,您使用仅包含该单个 group 的新 dict 覆盖session key 的 value,因此所有 session 都以最后一个 group 结束。 You probably want to replace the inner loop with a dictionary comprehension.您可能想用字典理解替换内部循环。

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