I have scraped 3 different websites and obtained 3 different values with corresponding keys. I want to divide these values by 2 before I pass them through Venmo to make a request. However, I have not figured out a way to do this in a way that will result in halving these amounts.
My current dictionary outputs as such:
{'Water': '$0.00', 'Electric': '$42.78', 'Gas': '$272.65'}
At the end of the website scraping I was thinking to grab these keys and divide them by a dictionary that looks like this:
{'Water': '2', 'Electric': '2', 'Gas': '2'}
Before I do so, I have to pass the first dictionary as float
values.
Any ideas as to what the best way to do this is after having the dictionary established?
Thanks!
CodePudding user response:
You can chop off the first character to remove the dollar sign, and then transform the sliced string into a float:
data = {'Water': '$0.00', 'Electric': '$42.78', 'Gas': '$272.65'}
divisors = {'Water': '2', 'Electric': '2', 'Gas': '2'}
result = {key: float(value[1:]) / float(divisors[key]) for key, value in data.items()}
print(result) # Prints {'Water': 0.0, 'Electric': 21.39, 'Gas': 136.325}