How would calculate the average, in salaries= { "Ben" : '82,500', "Bob" : '83,750', "Mike": '90,000', "Ann" : '89,000', "Sue" : '80,000' }

Question:

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

Asked By: papadum

||

Answers:

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)
Answered By: pgrad

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()))
Answered By: wkeithvan
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.