有这样一个 list,怎么把列表中的字典进行排序? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
rogwan

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

  •  
  •   rogwan Nov 13, 2016 5818 views
    This topic created in 3455 days ago, the information mentioned may be changed or developed.

    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 ?

    20 replies    2016-11-14 17:57:38 +08:00
    aaronzjw
        1
    aaronzjw  
       Nov 13, 2016 via Android
    sort+lambda
    justou
        2
    justou  
       Nov 13, 2016   1
    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
        3
    rogwan  
    OP
       Nov 13, 2016
    @aaronzjw
    sorted(list, key=lambda student_score:student_score[1], reverse=True)[0:3]

    这样解决,出错,结果不对。。。
    pupboss
        4
    pupboss  
       Nov 13, 2016
    def score(s):
    return s['student_score']

    bar = sorted(foo, key = score)

    print(bar)
    rogwan
        5
    rogwan  
    OP
       Nov 13, 2016
    @justou 赞!刚查书去,二楼的方法也是书上推荐的标准做法
    aaronzjw
        6
    aaronzjw  
       Nov 13, 2016
    @rogwan print sorted(list, key=lambda student: student['student_score'])[-3:]
    Mutoo
        7
    Mutoo  
       Nov 13, 2016   2
    [:3] 这个表情好搞笑
    ipconfiger
        8
    ipconfiger  
       Nov 13, 2016
    @rogwan 居然还有这种书
    triostones
        9
    triostones  
       Nov 13, 2016
    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
        10
    rogwan  
    OP
       Nov 13, 2016
    @ipconfiger 就是 Python 核心编程那本书啊
    rogwan
        11
    rogwan  
    OP
       Nov 13, 2016
    @triostones 谢谢,这个方法也 OK ^-^
    staticor
        12
    staticor  
       Nov 13, 2016
    python cookbook 里肯定是提了 第 1 章.
    rogwan
        13
    rogwan  
    OP
       Nov 13, 2016
    @staticor 是,去翻了, 1.13 节有讲
    timeship
        14
    timeship  
       Nov 13, 2016
    楼上正确, sorted ( list, key=itemgetter )然后取前三个,好像很多书里都有讲过类似的 QAQ
    gemini
        15
    gemini  
       Nov 14, 2016
    top3 = sorted(list, key=lambda stu:int(stu['student_score']), reverse=True)[0:3]
    ericls
        16
    ericls  
       Nov 14, 2016
    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
        17
    rogwan  
    OP
       Nov 14, 2016
    @ericls 确实可以 lol
    hl
        18
    hl  
       Nov 14, 2016
    请大家参考 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
        19
    jayyjh  
       Nov 14, 2016
    sorted_dict = sorted(dict.items(), key=lambda item: item[1])
    shyrock
        20
    shyrock  
       Nov 14, 2016
    @hl 语法上看不是最简单的,难道用 heapq 的效率更高?
    About     Help     Advertise     Blog     API     FAQ     Solana     2636 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 58ms UTC 11:51 PVG 19:51 LAX 04:51 JFK 07:51
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86