2-3课程 如何根据字典中值得大小,对字典中的项排序
Posted botoo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2-3课程 如何根据字典中值得大小,对字典中的项排序相关的知识,希望对你有一定的参考价值。
testCase
某班英语成绩以字典形式存储为:{‘LeiLei‘:90,‘kim‘:88,......},根据成绩高地,计算学生排名。
from faker import Faker
from random import randint
faker =Faker(‘zh‘)
#随机构造30个学生名称和成绩
studentsScore = {faker.name():randint(50,100) for i in range(30)}
print(studentsScore)
#利用zip合并为,(score,name)的形式,再排序
name = studentsScore.keys()
score = studentsScore.values()
print(sorted(zip(score,name),reverse=True))
{‘俞秀芳‘: 94, ‘党建国‘: 73, ‘叔莹‘: 83, ‘漆桂芝‘: 52, ‘虞丹‘: 66, ‘太荣‘: 62, ‘赫健‘: 59, ‘訾玉英‘: 99, ‘琴博‘: 66, ‘从桂芝‘: 68, ‘赫倩‘: 81, ‘农玲‘: 67, ‘于想‘: 59, ‘万春梅‘: 57, ‘窦兰英‘: 88, ‘乜梅‘: 88, ‘汪旭‘: 94, ‘金成‘: 83, ‘法芳‘: 78, ‘穆杰‘: 51, ‘秦军‘: 50, ‘申杰‘: 96, ‘哈玉‘: 50, ‘席莉‘: 78, ‘白红梅‘: 86, ‘葛凯‘: 59, ‘父建‘: 94, ‘郦强‘: 53, ‘政坤‘: 75, ‘严玉‘: 51}
[(99, ‘訾玉英‘), (96, ‘申杰‘), (94, ‘父建‘), (94, ‘汪旭‘), (94, ‘俞秀芳‘), (88, ‘窦兰英‘), (88, ‘乜梅‘), (86, ‘白红梅‘), (83, ‘金成‘), (83, ‘叔莹‘), (81, ‘赫倩‘), (78, ‘法芳‘), (78, ‘席莉‘), (75, ‘政坤‘), (73, ‘党建国‘), (68, ‘从桂芝‘), (67, ‘农玲‘), (66, ‘虞丹‘), (66, ‘琴博‘), (62, ‘太荣‘), (59, ‘赫健‘), (59, ‘葛凯‘), (59, ‘于想‘), (57, ‘万春梅‘), (53, ‘郦强‘), (52, ‘漆桂芝‘), (51, ‘穆杰‘), (51, ‘严玉‘), (50, ‘秦军‘), (50, ‘哈玉‘)]
[Finished in 0.3s]
以上是关于2-3课程 如何根据字典中值得大小,对字典中的项排序的主要内容,如果未能解决你的问题,请参考以下文章
python 小技巧, 如何根据字典中的值的大小,对字典中的项排序