Home > other >  get common elements in nested dictionaries
get common elements in nested dictionaries

Time:12-24

I am going to get the intersection of lists for each key. My dictionaries are:

dict_1 = {101: [['14', '02', '03', '07', '11'], ['04', '12', '05', '06', '08'], ['10', '16', '09', '13', '01']],
          102: [['21', '19', '14', '07', '10'], ['11', '04', '09', '12', '13'], ['03', '02', '15', '08', '17']]
}

dict_2 = {101: [['12', '02', '05', '01', '16'], ['04', '03', '18', '10', '14'], ['10', '11', '08', '07', '13']],
          102: [['21', '19', '11', '07', '15'], ['08', '03', '09', '12', '13'], ['03', '02', '15', '10', '17']]
}

What I mean by intersection (for 101):

list_1 = Common elements in ['14', '02', '03', '07', '11'] and ['12', '02', '05', '01', '16']
list_2 = Common elements in ['14', '02', '03', '07', '11'] and ['04', '03', '18', '10', '14']
list_3 = Common elements in ['14', '02', '03', '07', '11'] and ['10', '11', '08', '07', '13']
list_4 = Common elements in ['04', '12', '05', '06', '08'] and ['12', '02', '05', '01', '16']
list_5 = Common elements in ['04', '12', '05', '06', '08'] and ['04', '03', '18', '10', '14']
list_6 = Common elements in ['04', '12', '05', '06', '08'] and ['10', '11', '08', '07', '13']
list_7 = Common elements in ['10', '16', '09', '13', '01'] and ['12', '02', '05', '01', '16']
list_8 = Common elements in ['10', '16', '09', '13', '01'] and ['04', '03', '18', '10', '14']
list_9 = Common elements in ['10', '16', '09', '13', '01'] and ['10', '11', '08', '07', '13']

Final output should be like this:

output = {101: [['02'], ['14', '03'], ['07', '11'], ['12', '05'], ['04'], ['08'], ['16', '01'], ['10'], ['10', '13']], 
          102: [['21', '19', '07'], [], ['10'], ['11'], ['09', '12', '13'], [], ['15'], ['03', '08'], ['03', '02', '15', '17']]
}

What is the best way to do that?

I can get all the common elements of two lists but I am not able to do that in nested dictionaries.

CodePudding user response:

You can do the following:

output = {}
for k1, v1 in dict_1.items():
    v2 = dict_2[k1]
    temp = []
    for sub_list1 in v1:
        for sub_list2 in v2:
            temp.append(list(set(sub_list1).intersection(sub_list2)))
    output[k1] = temp

print(output)

Basically you iterate over dict_1's value which is a list itself. For each sublist, you iterate through the sublists of the values of dict_2. With intersection you can get the common items.

This could be written as a dictionary comprehension if you prefer. Here is the complete code:

dict_1 = {
    101: [["14", "02", "03", "07", "11"],["04", "12", "05", "06", "08"],["10", "16", "09", "13", "01"],],
    102: [["21", "19", "14", "07", "10"], ["11", "04", "09", "12", "13"], ["03", "02", "15", "08", "17"]],
}

dict_2 = {
    101: [["12", "02", "05", "01", "16"],["04", "03", "18", "10", "14"],["10", "11", "08", "07", "13"],],
    102: [["21", "19", "11", "07", "15"],["08", "03", "09", "12", "13"],["03", "02", "15", "10", "17"],],
}

res = {
    k1: [
        list(set(sub_list1).intersection(sub_list2))
        for sub_list1 in v1
        for sub_list2 in dict_2[k1]
    ]
    for k1, v1 in dict_1.items()
}
print(res)

Note: Since I used set operation to get the common keys, the order of the common keys like ['14', '03'] is not guaranteed. If that's also important to you, you should use another nested loop to check membership testing:

res = {
    k1: [
        [item for item in sub_list1 if item in sub_list2] # <-- Here
        for sub_list1 in v1
        for sub_list2 in dict_2[k1]
        
    ]
    for k1, v1 in dict_1.items()
}
print(res)
  • Related