teamzealot
teamzealot

Reputation: 71

Take common values in nested dictionary

I have a nested dictionary, how can I get the values with common key values such as 37, 74, etc:

myDict = {0: {37.0:  {'C23H27O9': 'C22H23O10'}},
          1: {74.0:  {'C23H27O9': 'C21H19O11'}},
          2: {111.0: {'C23H27O9': 'C20H15O12'}},
          3: {148.0: {'C23H27O9': 'C19H11O13'}},
          4: {37.0:  {'C22H23O10': 'C21H19O11'}},
          5: {74.0:  {'C22H23O10': 'C20H15O12'}},
          6: {111.0: {'C22H23O10': 'C19H11O13'}},
          7: {37.0:  {'C21H19O11': 'C20H15O12'}},
          8: {74.0:  {'C21H19O11': 'C19H11O13'}},
          9: {37.0:  {'C20H15O12': 'C19H11O13'}}
         }

Desired output:

37.0 --> C23H27O9: C22H23O10: C21H19O11 : C20H15O12 : C19H11O13
74.0 --> C23H27O9 : C21H19O11 : C19H11O13
...

Upvotes: 2

Views: 357

Answers (2)

Ajax1234
Ajax1234

Reputation: 71451

You can use itertools.groupby:

from itertools import groupby as gb
myDict = {0: {37.0: {'C23H27O9': 'C22H23O10'}}, 1: {74.0: {'C23H27O9': 'C21H19O11'}}, 2: {111.0: {'C23H27O9': 'C20H15O12'}}, 3: {148.0: {'C23H27O9': 'C19H11O13'}}, 4: {37.0: {'C22H23O10': 'C21H19O11'}}, 5: {74.0: {'C22H23O10': 'C20H15O12'}}, 6: {111.0: {'C22H23O10': 'C19H11O13'}}, 7: {37.0: {'C21H19O11': 'C20H15O12'}}, 8: {74.0: {'C21H19O11': 'C19H11O13'}}, 9: {37.0: {'C20H15O12': 'C19H11O13'}}}
d = sorted([(a, [j for k in b.items() for j in k]) for i in myDict.values() for a, b in i.items()], key=lambda x:x[0])
r = [(a, [*{i for _, j in b for i in j}]) for a, b in gb(d, key=lambda x:x[0])]

Output:

[(37.0, ['C19H11O13', 'C20H15O12', 'C21H19O11', 'C22H23O10', 'C23H27O9']), (74.0, ['C19H11O13', 'C20H15O12', 'C21H19O11', 'C22H23O10', 'C23H27O9']), (111.0, ['C20H15O12', 'C19H11O13', 'C23H27O9', 'C22H23O10']), (148.0, ['C23H27O9', 'C19H11O13'])]

Upvotes: 1

Matt Hall
Matt Hall

Reputation: 8142

It looks like you want the set of all keys and values from the innermost dictionaries, preserving the order in which they are encountered.

This seems like it does the job:

from collections import defaultdict

result = defaultdict(list)
for _, outerDict in myDict.items():
    for innerKey, innerDict in outerDict.items():
        for k, v in innerDict.items():
            if k not in result[innerKey]:
                result[innerKey].append(k)
            if v not in result:
                result[innerKey].append(v)

After this, result is a dict of lists:

>>> for key, values in result.items():
>>>    print(key, values)

37.0 ['C23H27O9', 'C22H23O10', 'C21H19O11', 'C20H15O12', 'C19H11O13']
74.0 ['C23H27O9', 'C21H19O11', 'C22H23O10', 'C20H15O12', 'C19H11O13']
111.0 ['C23H27O9', 'C20H15O12', 'C22H23O10', 'C19H11O13']
148.0 ['C23H27O9', 'C19H11O13']

You can format that output however you like, my assumption is that you just want the data.

Upvotes: 1

Related Questions