简体   繁体   English

计算列表中字典值的中位数

[英]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: output 看起来像:

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.我想我需要根据分数对 my_dict 进行排序,然后计算中值。 Can't quite figure out either step without using an exterior package.如果不使用外部 package,则无法完全弄清楚这两个步骤。

Any help would be greatly appreciated.任何帮助将不胜感激。 New to Python. 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: 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: output:

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

You can just use numpy.median to calculate the median.您可以只使用 numpy.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:如果您不想使用 package 并编写自己的 function,您可以这样称呼:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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