简体   繁体   中英

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. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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