import functools
# from functools import cmp_to_key
students = [{"name": 'eric', "score": [70, 50, 40]},
{"name": 'ben', "score": [70, 90, 40]},
{"name": 'gavin', "score": [70, 100, 189]}
]
def func_a(x, y):
return sum(x["score"]) - sum(y["score"])
result = sorted(students, key=functools.cmp_to_key(func_a))
print(result)
python_函数式编程_sorted_按照成绩总和对学生信息报进行排序
最新推荐文章于 2024-12-10 23:36:31 发布
本文介绍了一种使用Python中的functools模块对学生成绩数据进行排序的方法。通过定义一个比较函数func_a,该函数计算并比较学生总成绩,然后利用sorted函数和cmp_to_key转换器实现学生列表的排序。
1570

被折叠的 条评论
为什么被折叠?



