V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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
cheerzeng
V2EX  ›  Python

请教,一行代码进行排序。

  •  
  •   cheerzeng · 2015-04-24 11:38:51 +08:00 · 3466 次点击
    这是一个创建于 3286 天前的主题,其中的信息可能已经有所发展或是发生改变。
    怎么用python一行代码对下面的列表进行排序?
    l = [{'name':'n0','value': 93}, {'name':'n1','value': 90}, {'name':'n2','value': 95}, {'name':'n3','value': 93}, {'name':'n4','value': 90}]
    10 条回复    2015-04-26 12:22:00 +08:00
    codegear
        1
    codegear  
       2015-04-24 11:43:18 +08:00
    不知楼主的排序规则。大体上可以sort(l, key=...)
    ledzep2
        2
    ledzep2  
       2015-04-24 11:43:19 +08:00
    l.sort(key=lambda x:x['value'])
    hahastudio
        3
    hahastudio  
       2015-04-24 11:46:06 +08:00
    按什么排序?
    list.sort = sort(...)
    L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
    cmp(x, y) -> -1, 0, 1
    或者用 list(sorted( ... ))
    sorted(...)
    sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list

    比如
    >>> l.sort(key=lambda i:i['value'])
    >>> l
    [{'name': 'n1', 'value': 90}, {'name': 'n4', 'value': 90}, {'name': 'n0', 'value': 93}, {'name': 'n3', 'value': 93}, {'name': 'n2', 'value': 95}]
    cheerzeng
        4
    cheerzeng  
    OP
       2015-04-24 15:24:50 +08:00
    @codegear @ledzep2 @hahastudio 就是分数高低的规则。

    原来直接用sort就可以啦,我还在想怎么把排序算法搞进去呢。
    hahastudio
        5
    hahastudio  
       2015-04-24 15:29:15 +08:00
    @cheerzeng one line quick sort 也是经典的 snippet,虽然只能用来炫技
    qsort = lambda l: [] if not l else qsort([x for x in l[1:] if x <= l[0]]) + [l[0]] + qsort([x for x in l[1:] if x > l[0]])
    cheerzeng
        6
    cheerzeng  
    OP
       2015-04-24 16:53:36 +08:00
    @hahastudio 这个我也看到了,是不是除了快速排序,就不可能用其他算法放一行代码里了?
    hahastudio
        7
    hahastudio  
       2015-04-24 16:55:56 +08:00
    @cheerzeng https://code.activestate.com/recipes/578909-one-liner-sort-method-modified-selection-sort/
    一行除了炫技,真没什么用= =
    极端点的话,Python 还是有 ; 的
    cheerzeng
        8
    cheerzeng  
    OP
       2015-04-24 17:04:50 +08:00
    @hahastudio 回头看看,谢谢啊~
    pupboss
        9
    pupboss  
       2015-04-24 17:15:31 +08:00
    OC 做过类似的

    ```
    NSArray *arr = [self.planArr sortedArrayUsingComparator:^NSComparisonResult(ExamPlan *obj1, ExamPlan *obj2) {

    NSComparisonResult result = [obj1.startTime compare:obj2.startTime];
    return result;
    }];
    ```
    cheerzeng
        10
    cheerzeng  
    OP
       2015-04-26 12:22:00 +08:00 via Android
    @pupboss 昨天忘记回复你了。
    没学过OC,所以看不懂,不过还是谢谢你的分享^ω^
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1467 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 23:59 · PVG 07:59 · LAX 16:59 · JFK 19:59
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.