Reputation: 143
I have 2 python dictionaries, and each dictionary has a city name and a score of that city. I need to compare both the dictionaries in order to find the city with max score.Hence, for this I first take intersection of both the dictionaries to get common cities.This is where I am facing issues. For example, lets say the two dictionaries are:
d1 = {"delhi": 40, "Jaipur": 50, "Gurgaon": 10}
d2 = {"Jaipur(Rajasthan)": 30, "Gurugram(Gurgaon)": 25}
Here because of brackets or the city has some extra string along with it, the intersection fails.
So my question is , Is there any way where in if a city is present partly in a string, it is taken into the intersection?
Also, in the end I need to give the city an average score.
I want the end result to be:
d3 = {"gurgaon": 17.5((10 + 25) / 2), "jaipur": 40(80 / 2)}
How would I achieve this?
Upvotes: 0
Views: 74
Reputation: 106936
You can create normalized dicts where the keys used for matching are extracted from the original keys. Since names both inside and outside parentheses in the keys of the input dicts can be used for matching, create redundant keys for both names in the normalized dict:
import re
n1, n2 = (
{t.lower(): v for k, v in d.items() for t in re.findall('[^()]+', k)}
for d in (d1, d2)
)
print({k: (n1[k] + n2[k]) / 2 for k in n1.keys() & n2.keys()})
This outputs:
{'gurgaon': 17.5, 'jaipur': 40.0}
Upvotes: 2
Reputation: 1158
If you only have to compare two dicts you can do something like this using the filter function:
def get_avg_scores(d1, d2):
d3 = {}
for key, item in d1.items():
# Get match key d1 vs. d2
d2_similar_key = list(filter(lambda x: key.lower() in x.lower(), d2.keys()))
#Get match key d2 vs. d1
d2_similar_key_rev = list(filter(lambda x: x.lower() in key.lower(), d2.keys()))
# Keep the simplest key (to avoid bracets in d3)
if len(d2_similar_key) > 0:
d3[key] = (item + d2[d2_similar_key[0]])/2
if len(d2_similar_key_rev) > 0:
d3[d2_similar_key_rev[0]] = (item + d2[d2_similar_key_rev[0]])/2
return d3
d3 = get_avg_scores(d1, d2)
Upvotes: 1