Home > front end >  match values between 2 dictionaries & append the matching value to the new dictionary
match values between 2 dictionaries & append the matching value to the new dictionary

Time:03-18

I am trying to generate a table to compare between gold values & predicted values. The gold & predicted values are represented in 2 separate list of dictionaries which have different values & sizes.

What I would like to do is: If the id value of a dictionary in the gold list exists in the predicted list of dictionaries, append the gold detected label to the predicted list as a new key:value as 'gold label: True or False.

Note: the gold data contains only all of the actual True values, if there is a False entry in the predicted which is not existing in the gold data, then that is considered as a True Negative, hence both gold & predicted have False for value. Therefore the gold has less total values than predicted.

gold = [{'id': 'ABC', 'detected': True}, 
        {'id':'DEF', 'detected': True}] #contains only all the True values.

predicted = [{'id': 'ABC', 'detected': True}, 
             {'id':'DEF', 'detected': False}, 
             {'id':'GHI', 'detected': True}, 
             {'id':'JKL', 'detected': False}]


for p in predicted:
    if gold in predicted:
        p['gold label'] = True
    else:
        p['gold label'] = False

print(predicted)

current output:

[{'id': 'ABC', 'detected': True, 'gold label': False}, #not correct
 {'id': 'DEF', 'detected': False, 'gold label': False}, #not correct
 {'id': 'GHI', 'detected': True, 'gold label': False},#correct
 {'id': 'JKL', 'detected': False, 'gold label': False}] #correct 

desired output:

[{'id': 'ABC', 'detected': True, 'gold label': True}, #correct, match
 {'id': 'DEF', 'detected': False, 'gold label': True}, #correct, mismatch
 {'id': 'GHI', 'detected': True, 'gold label': False}, #correct, mismatch
 {'id': 'JKL', 'detected': False, 'gold label': False}] #correct, match

As you can see, I was only able to check if the gold dictionary exists in the predicted list of dictionaries, which would represent a "True" added value. But then it does not consider other options, such as false positives. Any hints so that I can proceed? Should I iterate on both list of dictionaries with nested loops?

CodePudding user response:

IIUC, you can first generate a dictionary of the gold id:detected pairs, then iterate:

gold_ids = {d['id']: d['detected'] for d in gold}
# {'ABC': True, 'DEF': True}

for p in predicted:
    p['gold_label'] = gold_ids.get(p['id'], False)

output:

[{'id': 'ABC', 'detected': True, 'gold_label': True},
 {'id': 'DEF', 'detected': False, 'gold_label': True},
 {'id': 'GHI', 'detected': True, 'gold_label': False},
 {'id': 'JKL', 'detected': False, 'gold_label': False}]
  • Related