jeudi 9 septembre 2021

How to implement logic on list of dictionaries based on multiple key value pairings?

I have a list of dictionaries below:

d_dict = [
    {'Abs Diff': 10,'Difference':10, 'Component': 'Local Market', 'Id':'123', 'Name': 'LAT', 'Executor':'AB'}, 
    {'Abs Diff': 20,'Difference':20, 'Component': 'Local Market', 'Id':'123', 'Name': 'LAT', 'Executor':'B'},
    {'Abs Diff': 30,'Difference':-30, 'Component': 'Local Market', 'Id':'123', 'Name': 'LAT', 'Executor':'BA'},
    {'Abs Diff': 23,'Difference':23, 'Component': 'USD Market', 'Id':'456', 'Name': 'LAT', 'Executor':'CB'}, 
    {'Abs Diff': 25,'Difference':25, 'Component': 'USD Market', 'Id':'456', 'Name': 'LAT', 'Executor':'C'},
    {'Abs Diff': 48,'Difference':-48, 'Component': 'USD Market', 'Id':'456', 'Name': 'LAT', 'Executor':'CB'},
    {'Abs Diff': 5,'Difference':5, 'Component': 'Price', 'Id':'123', 'Name': 'LAT', 'Executor':'AB'}, 
    {'Abs Diff': 5,'Difference':5, 'Component': 'Price', 'Id':'123', 'Name': 'LAT', 'Executor':'B'},
    {'Abs Diff': 10,'Difference':-10, 'Component': 'Price', 'Id':'123', 'Name': 'LAT', 'Executor':'BA'}
    ]

My end goal is to have a dictionary that returns:

result = [
    {'Abs Diff': 10,'Difference':10, 'Component': 'Local Market', 'Id':'123', 'Name': 'LAT', 'Executor':'AB', 'Result':'Mismatched Executor'}, 
    {'Abs Diff': 20,'Difference':20, 'Component': 'Local Market', 'Id':'123', 'Name': 'LAT', 'Executor':'B', 'Result':'Mismatched Executor'},
    {'Abs Diff': 30,'Difference':-30, 'Component': 'Local Market', 'Id':'123', 'Name': 'LAT', 'Executor':'BA', 'Result':'Mismatched Executor'},
    {'Abs Diff': 23,'Difference':23, 'Component': 'USD Market', 'Id':'456', 'Name': 'LAT', 'Executor':'CB', 'Result':'Mismatched Executor'}, 
    {'Abs Diff': 25,'Difference':25, 'Component': 'USD Market', 'Id':'456', 'Name': 'LAT', 'Executor':'C', 'Result':'Mismatched Executor'},
    {'Abs Diff': 48,'Difference':-48, 'Component': 'USD Market', 'Id':'456', 'Name': 'LAT', 'Executor':'CB', 'Result':'Mismatched Executor'},
    {'Abs Diff': 5,'Difference':5, 'Component': 'Price', 'Id':'123', 'Name': 'LAT', 'Executor':'AB', 'Result':'Mismatched Executor'}, 
    {'Abs Diff': 5,'Difference':5, 'Component': 'Price', 'Id':'123', 'Name': 'LAT', 'Executor':'B', 'Result':'Mismatched Executor'},
    {'Abs Diff': 10,'Difference':-10, 'Component': 'Price', 'Id':'123', 'Name': 'LAT', 'Executor':'BA', 'Result':'Mismatched Executor'}
]

I want to implement a logic that goes: if the Abs Diff is greater than 0 and if the component, id, name MATCH across multiple elements and if the sum of the differences across the same elements is less than 1 and the executor is different across those elements, return a new key value pairing "Result": "Mismatched"

I am having trouble visualizing how to scan through multiple dictionaries within the list and check for the matching component, ids, and names.

Aucun commentaire:

Enregistrer un commentaire