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

Time:09-17

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.

CodePudding user response:

Call sum() with a generator that finds all the dictionaries that match the criteria.

for i in d_dict:
    if i['Abs Diff'] > 0 and \
        sum(x['Difference'] for x in d_dict 
            if (x['Component'], x['Id'], x['Name']) == (i['Component'], i['Id'], i['Name']) and x['Executor'] != i['Executor']
        ) < 1:
        i['Result'] = 'Mismatched Executor'

CodePudding user response:

Heres an example of how I would solve this problem

from operator import itemgetter

newdict = []
for i in d_dict:
    if(i['Abs Diff'] > 0):
        newdict.append(i)
newlist = sorted(newdict, key=itemgetter('Component'))         
for i in range(0,len(d_dict)-1):
    if(d_dict[i]['Component'] == d_dict[int(i 1)]['Component']):
        print("TRUE")
        
  • Related