marlon
marlon

Reputation: 7693

How to find same-value entries in a dict and select the entry with highest score?

If I have a dict to store an id and an integer score, and another dict to store id and name. Two ids may have the same name, such as 'abc' & 'pqr' both having 'John' as their names. I want to find entries from id_score_dict with same names and only retain the entry with highest score.

id_score_dict = {'abc': 1, 'bcd': 2, 'mnl': 3, 'pqr': 0}

name_dict = {'abc': 'John', 'bcd': 'Mary', 'mnl': 'Cathy', 'pqr': 'John'}

for id, score in id_score_dict.items():
   name = name_dict[id]
   ...

For each iteration, the name=name_dict[id] would be updated. How to keep multiple names to compare with each other? For the example above, the id_score_dict after the identification operation would be:

id_score_dict = {'abc': 1, 'bcd': 2, 'mnl': 3}

'pqr' is removed because it has the same name as 'abc', but with a lower score of '0'. How to solve this problem? I don't know how to identify two ids with same names in the two dicts.

Upvotes: 0

Views: 36

Answers (1)

pho
pho

Reputation: 25489

You could create a new dictionary where the names are the keys. In this dict, store the id and the score as a tuple

new_dict = dict()
for p_id, name in name_dict.items():
    # If name is already in new_dict, and
    # the previous score is greater than the current score
    if name in new_dict and new_dict[name][1] > id_score_dict[p_id]:
        # Do nothing
        pass
    else: # Name not in new_dict, or current score is greater
        # Add current item
        new_dict[name] = (p_id, id_score_dict[p_id])

At this point, new_dict looks like this:

{
   'John': ('abc', 1),
   'Mary': ('bcd', 2),
   'Cathy': ('mnl', 3)
}

Finally, create a dict out of the .values() in new_dict

id_score_dict_new = dict(new_dict.values())

And now we have what we want:

{'abc': 1, 'bcd': 2, 'mnl': 3}

Upvotes: 1

Related Questions