Home > OS >  Finding the difference in values of matching keys in two dictonaries in python3
Finding the difference in values of matching keys in two dictonaries in python3

Time:09-21

So I have two dictionaries that store specific "channels" and their values, these channels update every second. My end goal is to create a tool/GUI that will help us determine how off or misaligned our channels are, they correspond to optical mount DOFs.

I currently have a function that grabs the current time and determines the channels value at that time and puts it in a dictionary and another function that lets you select a time and then does the same.

The part of this project I'm a bit lost on is how to compare these dictionaries.

I want to match the keys (which are my channels) and then find the difference between their two values and store that in a 3rd dictionary with the same channels.

So here's the basic idea in code for what I'm looking to do:

diff_channels_dict = {}
### After my functions that create these dictionaries run, I'll have something like the following, but a lot longer

ref_channels_dict = {

    'IM1_M1_P_OFFSET': 8,
    'IM1_M1_Y_OFFSET': 8,
    'IM2_M1_P_OFFSET': 8}
now_channels_dict = {

    'IM1_M1_P_OFFSET': 2,
    'IM1_M1_Y_OFFSET': 2,
    'IM2_M1_P_OFFSET': 2}


def some_func():

    for key in now_channels_dict and ref_channels_dict:
        diff = now_channels_dict[value] - ref_channels_dict[value]
        diff_channels_dict[key].append(diff)
    return

some_func()

### So then
diff_channels_dict = {

    'IM1_M1_P_OFFSET': -6,
    'IM1_M1_Y_OFFSET': -6,
    'IM2_M1_P_OFFSET': -6}

Can anyone give me some guidance on how to go about doing this? I looked into a few libraries, deepdiff, and Dictdiffer but I do not have access to them with the env that is used on all the machines that will use this tool.

CodePudding user response:

I'm going to assume that the starting dictionaries are like this (written in python format):

ref_channels_dict = {
            'IM1_M1_P_OFFSET': 8,
            'IM1_M1_Y_OFFSET': 8,
            'IM2_M1_P_OFFSET': 8
            }
            
now_channels_dict = {
            'IM1_M1_P_OFFSET': 2,
            'IM1_M1_Y_OFFSET': 2,
            'IM2_M1_P_OFFSET': 2
            }

From this, we can write the function that compares and put the differences into a new dictionary:

def compare_dictionaries(ref_dict, now_dict):
    assert ref_dict.keys() == now_dict.keys() # check if the keys are the same

    diff_dict = {}

    for key in ref_dict.keys(): #iterate over the keys and fill the diff_dict with the differences
        # this can be done because we know that the keys in both dictionaries are the same
        diff_dict[key] = now_dict[key] - ref_dict[key]

    return diff_dict

After that, we can define the whole code as:


def compare_dictionaries(ref_dict, now_dict):
    assert ref_dict.keys() == now_dict.keys() # check if the keys are the same

    diff_dict = {}

    for key in ref_dict.keys(): #iterate over the keys and fill the diff_dict with the differences
        # this can be done because we know that the keys in both dictionaries are the same
        diff_dict[key] = now_dict[key] - ref_dict[key]

    return diff_dict





if __name__ == "__main__":
    ref_channels_dict = {
            'IM1_M1_P_OFFSET': 8,
            'IM1_M1_Y_OFFSET': 8,
            'IM2_M1_P_OFFSET': 8
            }

    now_channels_dict = {
            'IM1_M1_P_OFFSET': 2,
            'IM1_M1_Y_OFFSET': 2,
            'IM2_M1_P_OFFSET': 2
            }

    print(compare_dictionaries(ref_channels_dict, now_channels_dict))

Which outputs:

>>> {'IM1_M1_P_OFFSET': -6, 'IM1_M1_Y_OFFSET': -6, 'IM2_M1_P_OFFSET': -6}

CodePudding user response:

with pandas you can do it without looping, what could be quite useful for really large dataset:

import pandas as pd

diff_dict = (pd.Series(now_channels_dict) - pd.Series(ref_channels_dict)).to_dict()

print(diff_dict)  
'''
{'IM1_M1_P_OFFSET': -6, 'IM1_M1_Y_OFFSET': -6, 'IM2_M1_P_OFFSET': -6}
  • Related