Home > Net >  Comparing the values of common keys in multiple Nested Dictionaries
Comparing the values of common keys in multiple Nested Dictionaries

Time:05-18

Below is the Input_dict for data analysis:

input_dict =
 { 
    "C:\\arm64\\lib_apple.so": { "func-abc": [5,6,7,8], "func-123":[1,1,1,1] },
    "C:\\arm64\\lib_banana.so": { "func-123": [2,3,4], "func-rt": [0,0] },
    "C:\\armeabi\\lib_banana.so": { "func-123": [1,0,0], "func-rt": [1,5] },
    "C:\\armeabi\\lib2.so": { "func-0": [1]},
    "C:\\x86\\lib_apple.so": { "func-so": [5,6,7,8], "func-123": [2,2,1,1] },
    "C:\\x86\\lib_banana.so": { "func-rt": [2,0] }, 
    "C:\\x86\\lib2.so": { "func-0": [1,2,3]}
 }

The aim is to compare the 'values' of functions with same name of different architectures(arm64,armeabi,x86). In other words, I want to compare the "lists" of functions(with same name) in different libraries(.so) files.

For example: Comparing func-123: [2,3,4] with func-123: [1,0,0] from arm64\lib_banana.so and armeabi\lib_banana.so respectively.

One of the desired output could be:

{ lib_apple.so: { func-123: [arm64,[1,1,1,1]],[x86,[2,2,1,1]]}}

CodePudding user response:

You can restructure your function data to order by their name first, then supported architectures. Afterwards, print out those functions that appear in multiple architectures:

from collections import defaultdict
from pathlib import PureWindowsPath

lib2func = {
    r'C:\arm64\lib_apple.so': { 'func-abc': [5,6,7,8], 'func-123': [1,1,1,1] },
    r'C:\arm64\lib_banana.so': { 'func-123': [2,3,4], 'func-rt': [0,0] },
    r'C:\armeabi\lib_banana.so': { 'func-123': [1,0,0], 'func-rt': [1,5] },
    r'C:\armeabi\lib.so': {},
    r'C:\armeabi\lib2.so': { 'func-0': [1]},
    r'C:\x86\lib_apple.so': { 'func-so': [5,6,7,8], 'func-123': [2,2,1,1] },
    r'C:\x86\lib_banana.so': { 'func-rt': [2,0] },
    r'C:\x86\lib2.so': { 'func-0': [1,2,3] },
}
# restructure
func2arch = defaultdict(dict)
for lib_path, functions in lib2func.items():
    path = PureWindowsPath(lib_path)
    lib = path.name
    arch = path.parent.name
    for func_name, func_val in functions.items():
        func2arch[(lib, func_name)][arch] = func_val

# find functions defined for multiple architectures
for (lib, func_name), arch_data in func2arch.items():
    if len(arch_data) < 2:
        continue # skip functions that only appear once
    print(lib, func_name, arch_data)

gives

lib_apple.so func-123 {'arm64': [1, 1, 1, 1], 'x86': [2, 2, 1, 1]}
lib_banana.so func-123 {'arm64': [2, 3, 4], 'armeabi': [1, 0, 0]}
lib_banana.so func-rt {'arm64': [0, 0], 'armeabi': [1, 5], 'x86': [2, 0]}
lib2.so func-0 {'armeabi': [1], 'x86': [1, 2, 3]}

The above code assumes that library/function name pairs are unique.

  • Related