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

dict list 按照另一个 list 排序

  •  
  •   wonder1z · Mar 13, 2019 · 2283 views
    This topic created in 2759 days ago, the information mentioned may be changed or developed.

    请教大神,有什么快速的排序方法吗 [{"a":3,"b":"ggg"},{"a":1,"b":"fff"},{"a":2,"b":"eee"}] 按照 [2,1,3] 排序 输出[{"a":2,"b":"eee"},{"a":1,"b":"fff"},{"a":3,"b":"ggg"}]

    3 replies  •  2019-03-13 14:17:56 +08:00
    myyou
        1
    myyou  
       Mar 13, 2019
    sorted([{"a":3,"b":"ggg"},{"a":1,"b":"fff"},{"a":2,"b":"eee"}], key=lambda x: [2, 1, 3].index(x["a"]))
    wonder1z
        2
    wonder1z  
    OP
       Mar 13, 2019
    @myyou 牛逼,多谢
    wqzjk393
        3
    wqzjk393  
       Mar 13, 2019
    有个比较特别的方法。读进 pandas.dataframe。df = pandas.dataframe(values=dict.value,names=dict.keys) df=df.sorted(by='a'),然后再包装成一个字典返回去
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2529 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 03:46 · PVG 11:46 · LAX 20:46 · JFK 23:46
    ♥ Do have faith in what you're doing.