Im having issues iterating through the dictionary because of the quotation marks in the values, how would I calculate the average in a dicitonary like this?
I tried to use the map function to bypass the quotation marks but that didnt work
CodePudding user response:
You need to first remove the ',' character from the strings then convert them to int
to perform the math. Python will coerce the int type once the ',' is removed but it is better to explicitly convert to catch any errors early.
salaries= { "Ben" : '82,500', "Bob" : '83,750', "Mike": '90,000', "Ann" : '89,000', "Sue" : '80,000' }
for k,v in salaries.items():
salaries[k] = int(v.replace(',',''))
avg = sum(salaries.values())/len(salaries.values())
print(avg)
CodePudding user response:
The locale
package can help you deal with converting strings with commas to integers (see this post for more help).
You can use the map
function to convert your dictionary strings into an iterable of proper integers by applying the locale.atoi
function to every value in the dictionary. Then you can use the mean
function in the statistics
package to compute the average of the list.
import locale
from statistics import mean
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
d = { "Ben" : '82,500', "Bob" : '83,750', "Mike": '90,000', "Ann" : '89,000', "Sue" : '80,000' }
avg = mean(map(locale.atoi, d.values()))
Keep in mind that you may have issues if you get data from Europe as they use commas and periods differently in numbers.
If you don't want to use locale
, you can do a similar thing with a lambda that does a string replace in the map function.
from statistics import mean
d = { "Ben" : '82,500', "Bob" : '83,750', "Mike": '90,000', "Ann" : '89,000', "Sue" : '80,000' }
avg = mean(map(lambda x: int(x.replace(',', '')), d.values()))
CodePudding user response:
First you'll have to remove the commas with the replace(",", "")
line. You'll have to convert the strings of digits into an int()
, float()
, or similar second. Then you can take those values and average them out as you see fit. In this case we ran the statistics.mean
, but statistics.fmean
works, or just sum(x) / len(x)
import statistics
salaries = {"a": "1,000", "b": "2,000"}
average = statistics.mean([float(salaries[value].replace(",", "")) for value in salaries])
print(average)