Finding mean of a values in a dictionary without using .values() etc

前端 未结 10 1353
挽巷
挽巷 2021-01-02 17:29

I have a dictionary that looks like:

G={\'E\': 18.0, \'D\': 17.0, \'C\': 19.0, \'B\': 15.0, \'A\': 0}

I have to find the mean of the values

10条回答
  •  耶瑟儿~
    2021-01-02 17:57

    You want:

    mean = sum([G[key] for key in G])/float(len(G))
    

    Your original code will also produce a:

    TypeError: 'int' object is not iterable
    

    when you try to sum the values.

提交回复
热议问题