python中sort排序函数用法,python中sorted方法排序规则
直接使用排序()函数分数=[78,90,40,60]分数。sort()print(score)# #[40,60,78,90]根据数据中的某一个项排序students=[{age: 18, name: jack , score: 89},{age: 15, name: tom , score: 98}, name: mike , score: 98},{age: 19, name: jone , score :95 }]学生。sort(key=lambda student:student[ score ])print(students)# #[{ age :18, name: jack , score: 89},# # # { age :19, name使用自定义的比较函数在Python2.x中,可以使用金属波纹管函数
students=[{age: 18, name: jack , score: 89},{age: 15, name: tom , score: 98},16, name: mike , score: 98},{age: 19, name: jone , score: 95}]def compare(a,b):if a[ score ]b[ score ]:return 1 elif a[ score ]b[ score ]:return-1 else:返回0个学生。排序(CMP=s在python3.x中,移除了金属波纹管函数,如果需要自定义比较,需要借用函数工具模块来完成
导入func tools students=[{ age :18, name: jack , score: 89},{age: 15, name: tom , score: 98},{ name : Mike , score: 98},{age: 19, name: jone , score: 95}]def compare(a,b):if a[ score ]b[ score ]:return 1 elif a[ score ]b[ score ]:return-1 else:返回0学生排序
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。