Home > OS >  How to subset Nested Dictionaries
How to subset Nested Dictionaries

Time:10-20

I have a dictionary like the following:

original = {"Triclusters":{"0":{"%Missings":"0","ColumnPattern":"Constant","Data":
                               {"0":[["-1.72","-1.72","-1.72","-1.72"],["-1.72","-1.72","-1.72","-1.72"]
                                     ,["-1.72","-1.72","-1.72","-1.72"],["-1.72","-1.72","-1.72",
                                                                         "-1.72"]],
                                "1":[["-1.72","-1.72","-1.72","-1.72"],["-1.72","-1.72","-1.72","-1.72"]
                                     ,["-1.72","-1.72","-1.72","-1.72"],["-1.72","-1.72","-1.72","-1.72"]],
                                "2":[["-1.72","-1.72","-1.72","-1.72"],["-1.72","-1.72","-1.72","-1.72"]
                                     ,["-1.72","-1.72","-1.72","-1.72"],["-1.72","-1.72","-1.72","-1.72"]]},
                               "#contexts":3,"PlaidCoherency":"No Overlapping","%Errors":"0","%Noise":"0",
                               "X":[0,2,3,4],"ContextPattern":"Constant","Y":[0,2,6,7],
                               "RowPattern":"Constant","Z":[0,1,2],"#rows":4,"#columns":4},
                          "1":{"%Missings":"0","ColumnPattern":"None","Data":{"0":[["-3.52","-9.34","-9.04","-2.56"],
                                                                                   ["-3.52","-9.34","-9.04","-2.56"]
                                                                                   ,["-3.52","-9.34","-9.04","-2.56"]
                                                                                   ,["-3.52","-9.34","-9.04","-2.56"]],
                                                                              "1":[["7.04","-2.13","2.04","5.09"],
                                                                                   ["7.04","-2.13","2.04","5.09"],
                                                                                   ["7.04","-2.13","2.04","5.09"],
                                                                                   ["7.04","-2.13","2.04","5.09"]],
                                                                              "2":[["2.17","5.93","-5.47","-8.74"],
                                                                                   ["2.17","5.93","-5.47","-8.74"],
                                                                                   ["2.17","5.93","-5.47","-8.74"],
                                                                                   ["2.17","5.93","-5.47","-8.74"]]},
                               "#contexts":3,"PlaidCoherency":"No Overlapping","%Errors":"0","%Noise":"0",
                               "X":[0,1,2,3],"ContextPattern":"None","Y":[1,3,4,9],"RowPattern":"Constant",
                               "Z":[0,1,2],"#rows":4,"#columns":4}},"#DatasetMinValue":-10,"#DatasetColumns":10,
           "#DatasetContexts":3,"#DatasetMaxValue":10,"#DatasetRows":5} 

and I want to obtain a dictionary like this:

new_dictionary = {"0":{"0":[["-1.72","-1.72","-1.72","-1.72"],["-1.72","-1.72","-1.72","-1.72"]
                                    ,["-1.72","-1.72","-1.72","-1.72"],["-1.72","-1.72","-1.72","-1.72"]],
                               "1":[["-1.72","-1.72","-1.72","-1.72"],["-1.72","-1.72","-1.72","-1.72"]
                                    ,["-1.72","-1.72","-1.72","-1.72"],["-1.72","-1.72","-1.72","-1.72"]],
                               "2":[["-1.72","-1.72","-1.72","-1.72"],["-1.72","-1.72","-1.72","-1.72"]
                                    ,["-1.72","-1.72","-1.72","-1.72"],["-1.72","-1.72","-1.72","-1.72"]]},
                          "1":{"0":[["-3.52","-9.34","-9.04","-2.56"],["-3.52","-9.34","-9.04","-2.56"],
                                    ["-3.52","-9.34","-9.04","-2.56"],["-3.52","-9.34","-9.04","-2.56"]],
                               "1":[["7.04","-2.13","2.04","5.09"],["7.04","-2.13","2.04","5.09"],
                                    ["7.04","-2.13","2.04","5.09"],["7.04","-2.13","2.04","5.09"]],
                               "2":[["2.17","5.93","-5.47","-8.74"],["2.17","5.93","-5.47","-8.74"],
                                    ["2.17","5.93","-5.47","-8.74"],["2.17","5.93","-5.47","-8.74"]]}}`

I don't have much experience on how to subset nested dictionaries, how can I do it?

CodePudding user response:

You could do it via:

d = {k: {nk: nv for nk, nv in v['Data'].items()} for k, v in original['Triclusters'].items()}

d
{'0': {'0': [['-1.72', '-1.72', '-1.72', '-1.72'], ['-1.72', '-1.72', '-1.72', '-1.72'], ['-1.72', '-1.72', '-1.72', '-1.72'], ['-1.72', '-1.72', '-1.72', '-1.72']], '1': [['-1.72', '-1.72', '-1.72', '-1.72'], ['-1.72', '-1.72', '-1.72', '-1.72'], ['-1.72', '-1.72', '-1.72', '-1.72'], ['-1.72', '-1.72', '-1.72', '-1.72']], '2': [['-1.72', '-1.72', '-1.72', '-1.72'], ['-1.72', '-1.72', '-1.72', '-1.72'], ['-1.72', '-1.72', '-1.72', '-1.72'], ['-1.72', '-1.72', '-1.72', '-1.72']]}, '1': {'0': [['-3.52', '-9.34', '-9.04', '-2.56'], ['-3.52', '-9.34', '-9.04', '-2.56'], ['-3.52', '-9.34', '-9.04', '-2.56'], ['-3.52', '-9.34', '-9.04', '-2.56']], '1': [['7.04', '-2.13', '2.04', '5.09'], ['7.04', '-2.13', '2.04', '5.09'], ['7.04', '-2.13', '2.04', '5.09'], ['7.04', '-2.13', '2.04', '5.09']], '2': [['2.17', '5.93', '-5.47', '-8.74'], ['2.17', '5.93', '-5.47', '-8.74'], ['2.17', '5.93', '-5.47', '-8.74'], ['2.17', '5.93', '-5.47', '-8.74']]}}

CodePudding user response:

Try this:

new_dictionary = {key: value['Data'] for key, value in original['Triclusters'].items()} 

CodePudding user response:

You can use a dictionary comprehension as such :

new_dictionary = {index: tricluster["Data"] for index, tricluster in original["Triclusters"].items()}
  • Related