有这样一个 list,怎么把列表中的字典进行排序?

2016-11-13 15:02:37 +08:00
 rogwan

list = [ {'student_name': zhangsan, 'student_score': 65}, {'student_name': lisi, 'student_score': 95}, {'student_name': wangwu, 'student_score': 80}, {'student_name': maliu, 'student_score': 75}, {'student_name': zhuqi, 'student_score': 88} ]

把 5 个学生成绩从高到低排序,取前三名,要怎么处理这样的 list ?

5040 次点击
所在节点    Python
20 条回复
aaronzjw
2016-11-13 15:06:20 +08:00
sort+lambda
justou
2016-11-13 15:12:43 +08:00
from operator import itemgetter

lst = [ {'student_name': 'zhangsan', 'student_score': 65},
{'student_name': 'lisi', 'student_score': 95},
{'student_name': 'wangwu', 'student_score': 80},
{'student_name': 'maliu', 'student_score': 75},
{'student_name': 'zhuqi', 'student_score': 88} ]

top3 = sorted(lst, key=itemgetter('student_score'), reverse=True)[:3]

print top3
rogwan
2016-11-13 15:12:48 +08:00
@aaronzjw
sorted(list, key=lambda student_score:student_score[1], reverse=True)[0:3]

这样解决,出错,结果不对。。。
pupboss
2016-11-13 15:14:01 +08:00
def score(s):
return s['student_score']

bar = sorted(foo, key = score)

print(bar)
rogwan
2016-11-13 15:20:55 +08:00
@justou 赞!刚查书去,二楼的方法也是书上推荐的标准做法
aaronzjw
2016-11-13 17:30:27 +08:00
@rogwan print sorted(list, key=lambda student: student['student_score'])[-3:]
Mutoo
2016-11-13 17:34:34 +08:00
[:3] 这个表情好搞笑
ipconfiger
2016-11-13 17:48:56 +08:00
@rogwan 居然还有这种书
triostones
2016-11-13 18:01:40 +08:00
In [12]: import heapq

In [13]: from operator import itemgetter

In [14]: scores = [ {'student_name': 'zhangsan', 'student_score': 65}, {'student_name': 'lisi', 'student_score': 95}, {'student_name':'wangwu', 'student_score': 80}, {'student_name': 'maliu', 'student_sco
...: re': 75}, {'student_name': 'zhuqi', 'student_score': 88} ]

In [15]: heapq.nlargest(3, scores, key=itemgetter('student_score'))
Out[15]:
[{'student_name': 'lisi', 'student_score': 95},
{'student_name': 'zhuqi', 'student_score': 88},
{'student_name': 'wangwu', 'student_score': 80}]
rogwan
2016-11-13 18:47:49 +08:00
@ipconfiger 就是 Python 核心编程那本书啊
rogwan
2016-11-13 18:53:57 +08:00
@triostones 谢谢,这个方法也 OK ^-^
staticor
2016-11-13 19:33:46 +08:00
python cookbook 里肯定是提了 第 1 章.
rogwan
2016-11-13 20:19:07 +08:00
@staticor 是,去翻了, 1.13 节有讲
timeship
2016-11-13 21:51:29 +08:00
楼上正确, sorted ( list, key=itemgetter )然后取前三个,好像很多书里都有讲过类似的 QAQ
gemini
2016-11-14 00:00:26 +08:00
top3 = sorted(list, key=lambda stu:int(stu['student_score']), reverse=True)[0:3]
ericls
2016-11-14 00:07:08 +08:00
lst = [ {'student_name': 'zhangsan', 'student_score': 65},
{'student_name': 'lisi', 'student_score': 95},
{'student_name': 'wangwu', 'student_score': 80},
{'student_name': 'maliu', 'student_score': 75},
{'student_name': 'zhuqi', 'student_score': 88} ]


sorted(lst, key=lambda stu: stu["student_score"], reverse=True)[:3]

明明就可以
rogwan
2016-11-14 11:07:54 +08:00
@ericls 确实可以 lol
hl
2016-11-14 13:59:07 +08:00
请大家参考 python 官方高级数据结构 heapq 章节,可以使用内置高效的方法来最简单的实现

students_list = [ {'student_name': 'zhangsan', 'student_score': 65},
{'student_name': 'lisi', 'student_score': 95},
{'student_name': 'wangwu', 'student_score': 80},
{'student_name': 'maliu', 'student_score': 75},
{'student_name': 'zhuqi', 'student_score': 88} ]


####################
import heapq

score_first_3 = heapq.nlargest(3,students_list,key=lambda student:student["student_score"])

print score_first_3
####################
jayyjh
2016-11-14 15:55:29 +08:00
sorted_dict = sorted(dict.items(), key=lambda item: item[1])
shyrock
2016-11-14 17:57:38 +08:00
@hl 语法上看不是最简单的,难道用 heapq 的效率更高?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/320176

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX