如何在python中找到字典值的百分位数
我被问到以下问题,并且被要求不要使用numpy和pandas 考虑在两个列表中给出的班级学生的分数列表如何在python中找到字典值的百分位数,python,Python,我被问到以下问题,并且被要求不要使用numpy和pandas 考虑在两个列表中给出的班级学生的分数列表 Students =['student1','student2','student3','student4','student5','student6','student7','student8','student9','student10'] Marks = [45, 78, 12, 14, 48, 43, 45, 98, 35, 80] 从以上两个列表中,列出了Student[0]
Students =['student1','student2','student3','student4','student5','student6','student7','student8','student9','student10']
Marks = [45, 78, 12, 14, 48, 43, 45, 98, 35, 80]
从以上两个列表中,列出了Student[0]
gotMarks[0]
,Student[1]
gotMarks[1]
等等
所以我们的任务就是打印学生的名字
- 按分数的降序排列,谁获得了前5名
- 按照分数的递增顺序,谁获得了至少5个等级
- 谁的分数在>25%之间
使用上述函数通过提供已排序的标记列表来计算百分位数的值。然后根据百分位值过滤字典
上述功能的灵感来源于
使用上述函数通过提供已排序的标记列表来计算百分位数的值。然后根据百分位值过滤字典
上述函数的灵感来源于要限制为五个,请添加
以仅对排序的[:5]
列表的前五个元素进行交互
应该完成降序列表的工作 关于百分位数,这更像是一个统计问题,但你可以做:对于键,排序中的值(dictionary.items(),key=lambda item:item[1],reverse=True)[:5]:
n = len(marks) first_quartile = int(n/4) if (n/4).is_integer() else int(n/4) + 1 third_quartile = int(3*n/4) # as we want <75th percentile
n=len(标记) 第一个四分位数=int(n/4)if(n/4)。是整数()else int(n/4)+1
第三个四分位数=int(3*n/4)#正如我们希望限制为五个一样,添加
以仅对排序的[:5]
列表的前五个元素进行交互
应该完成降序列表的工作 关于百分位数,这更像是一个统计问题,但你可以做:对于键,排序中的值(dictionary.items(),key=lambda item:item[1],reverse=True)[:5]:
n = len(marks) first_quartile = int(n/4) if (n/4).is_integer() else int(n/4) + 1 third_quartile = int(3*n/4) # as we want <75th percentile
我认为您的代码存在错误,例如n=len(标记) 第一个四分位数=int(n/4)if(n/4)。是整数()else int(n/4)+1
第三个四分位数=int(3*n/4)#正如我们所希望的那样尝试一下这个,不要使用与代码类似的numpyfor key, value in sorted(dictionary.items(), key=lambda item: item[1]): percentage = value * 100.0 / 100 # I assumed 100 as total marks (You can change it if you want like sum(Marks)) if 25 < percentage < 75 : print("%s: %s : %d" % (key, value, percentage))
试试这个,不要使用与代码类似的numpyfor key, value in sorted(dictionary.items(), key=lambda item: item[1]): percentage = value * 100.0 / 100 # I assumed 100 as total marks (You can change it if you want like sum(Marks)) if 25 < percentage < 75 : print("%s: %s : %d" % (key, value, percentage))
下面是这个问题的完整答案Students=['student1','student2','student3','student4','student5','student6','student7','student8','student9','student10'] Marks = [45, 78, 12, 14, 48, 43, 47, 98, 35, 80] dictionary = dict(zip(Students,Marks)) ### to map students and marks for key, value in sorted(dictionary.items(), key=lambda item: item[1],reverse=True)[0:5]: print("%s: %s" % (key, value)) for key, value in sorted(dictionary.items(),key = lambda item: item[1])[0:5]: print("%s: %s" % (key, value)) for key, value in sorted(dictionary.items(), key=lambda item: item[1]): percentage = value * 100.0 / 100 # I assumed 100 as total marks (You can change it if you want like sum(Marks)) if 25 < percentage < 75 : print("%s: %d" % (key, value))
以下是这个问题的完整答案Students=['student1'、'student2'、'student3'、'student4'、'student5'、'student6'、'student7'、'student8'、'student9'、'student10'] 分数=[45,78,12,14,48,43,47,98,35,80] dictionary=dict(zip(学生、分数))35;###用于映射学生和分数 对于键,排序后的值(dictionary.items(),key=lambda item:item[1],reverse=True)[0:5]: 打印(“%s:%s”%(键,值)) 对于键,排序后的值(dictionary.items(),key=lambda item:item[1])[0:5]: 打印(“%s:%s”%(键,值)) 对于键,排序后的值(dictionary.items(),key=lambda item:item[1]): 百分比=值*100.0/100#我假设100分为总分(如果你喜欢总和,你可以更改它) 如果25<百分比<75: 打印(“%s:%d”%(键,值))
Students=['student1','student2','student3','student4','student5','student6','student7','student8','student9','student10'] Marks = [45, 78, 12, 14, 48, 43, 47, 98, 35, 80] dictionary = dict(zip(Students,Marks)) ### to map students and marks for key, value in sorted(dictionary.items(), key=lambda item: item[1],reverse=True)[0:5]: print("%s: %s" % (key, value)) for key, value in sorted(dictionary.items(),key = lambda item: item[1])[0:5]: print("%s: %s" % (key, value)) for key, value in sorted(dictionary.items(), key=lambda item: item[1]): percentage = value * 100.0 / 100 # I assumed 100 as total marks (You can change it if you want like sum(Marks)) if 25 < percentage < 75 : print("%s: %d" % (key, value))
如果出现连接标记,您希望如何处理?我认为您的代码存在错误,例如Students=['student1'、'student2'、'student3'、'student4'、'student5'、'student6'、'student7'、'student8'、'student9'、'student10'] 分数=[45,78,12,14,48,43,47,98,35,80] dictionary=dict(zip(学生、分数))35;###用于映射学生和分数 对于键,排序后的值(dictionary.items(),key=lambda item:item[1],reverse=True)[0:5]: 打印(“%s:%s”%(键,值)) 对于键,排序后的值(dictionary.items(),key=lambda item:item[1])[0:5]: 打印(“%s:%s”%(键,值)) 对于键,排序后的值(dictionary.items(),key=lambda item:item[1]): 百分比=值*100.0/100#我假设100分为总分(如果你喜欢总和,你可以更改它) 如果25<百分比<75: 打印(“%s:%d”%(键,值))
在函数中定义,但在dictionary=dict(zip(学生,标记))
如果出现连接标记,您希望如何处理?sorted(dictionary.items(),key=lambda item:item[1]中的函数外部使用
在函数中定义,但在dictionary=dict(zip(student,Marks))
同样,sorted(dictionary.items(),key=lambda item:item[1])中的函数外部使用。