简体   繁体   中英

Calculate median of dictionary values inside list

I'm looking to calculate the median of "score" (a dictionary value) inside a list.

my_dict = {"John": [{"class": "math", "score": 100, "year": 2014}, {"class": "english", "score": 85, "year": 2015}, {"class": "science", "score": 90, "year": 2015}], 
"Timmy": [{"class": "math", "score": 87, "year": 2014}, {"class": "english", "score": 91, "year": 2015}], 
"Sally":[{"class": "math", "score": 95, "year": 2014}]}

The output would look like:

new_dict = {"John": 90, "Timmy": 89, "Sally": 95}

I figured I need to sort my_dict based on score and then calculate the median value. Can't quite figure out either step without using an exterior package.

Any help would be greatly appreciated. New to Python.

One liner using median and dict comprehension

from statistics import median 
{k:median([v.get('score',0) for v in my_dict[k]]) for k in my_dict.keys()}

Output:

{'John': 90, 'Timmy': 89.0, 'Sally': 95}
my_dict = {"John": [{"class": "math", "score": 100, "year": 2014}, {"class": "english", "score": 85, "year": 2015}, {"class": "science", "score": 90, "year": 2015}], 
"Timmy": [{"class": "math", "score": 87, "year": 2014}, {"class": "english", "score": 91, "year": 2015}], 
"Sally":[{"class": "math", "score": 95, "year": 2014}]}

import numpy as np
medians = {}
for k in my_dict.keys():
    ls = []
    for d in my_dict[k]:
        ls.append(d['score'])
    medians[k] = np.median(ls)

print(medians)

output:

{'Sally': 95.0, 'Timmy': 89.0, 'John': 90.0}

You can just use numpy.median to calculate the median.

You can use the median from statistics .

from statistics import median

my_dict = {"John": [{"class": "math", "score": 100, "year": 2014}, {"class": "english", "score": 85, "year": 2015}, {"class": "science", "score": 90, "year": 2015}], 
"Timmy": [{"class": "math", "score": 87, "year": 2014}, {"class": "english", "score": 91, "year": 2015}], 
"Sally":[{"class": "math", "score": 95, "year": 2014}]}

new_dict = {}

for k, v in my_dict.items():
  m = []
  for l in v:
    m.append(l["score"])
  new_dict[k] = median(m)

print(new_dict)

If you don't want to use a package and write your own function, you can call this:

def median(lst):
    n = len(lst)
    s = sorted(lst)
    return (sum(s[n//2-1:n//2+1])/2.0, s[n//2])[n % 2] if n else None

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM