推荐学习书目
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
dwadewyp
V2EX  ›  Python

Python list 中的字段,想根据 value 中的一个 key 排序,如下代码,有什么优雅的写法么

  •  
  •   dwadewyp · Dec 4, 2020 · 3033 views
    This topic created in 2031 days ago, the information mentioned may be changed or developed.

    rd = [{100: {"id": 1, "name": "wade", "code": "abc", "is_analysis": 1}}, {101: {"id": 2, "name": "lebron", "code": "abc", "is_analysis": 2}}, {102: {"id": 3, "name": "cp3", "code": "abc", "is_analysis": 0}}] 根据 is_analysis 来排序,有什么优雅的写法么

    9 replies    2020-12-07 18:35:41 +08:00
    woostundy
        1
    woostundy  
       Dec 4, 2020
    sorted(rd, key=lambda x:x[is_analysis])
    lIlIlIlI
        2
    lIlIlIlI  
       Dec 4, 2020
    如果你保证 dict 只有一个 key 的话
    rd.sort(key=lambda x:x.values()[0]['is_analysis'])
    snachx
        3
    snachx  
       Dec 4, 2020
    rd.sort(key=lambda item: list(item.values())[0]['is_analysis'])
    wuwukai007
        4
    wuwukai007  
       Dec 4, 2020 via Android
    一楼明显不对🙄
    hengstchon
        5
    hengstchon  
       Dec 4, 2020   ❤️ 1
    我是蜻蜓队长,我宣布 1 楼和 2 楼不对,3 楼正确。
    fasionchan
        6
    fasionchan  
       Dec 5, 2020
    或许花点时间,停下来思考一下,这样的数据结构设计是否合理,会有更好的想法🤔
    luckyc
        7
    luckyc  
       Dec 5, 2020
    这样的结构简直莫名其妙
    noparking188
        8
    noparking188  
       Dec 5, 2020
    兄弟,这么复杂的字典数据上 dataclasses 叭
    shm7
        9
    shm7  
       Dec 7, 2020 via iPhone
    以后可能会有问 print string 的写法的了 % {} 还是 fstring 。坐等
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   931 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 79ms · UTC 19:09 · PVG 03:09 · LAX 12:09 · JFK 15:09
    ♥ Do have faith in what you're doing.