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
wonder1z
V2EX  ›  Python

dict list 按照另一个 list 排序

  •  
  •   wonder1z · 2019-03-13 13:49:00 +08:00 · 1501 次点击
    这是一个创建于 1864 天前的主题,其中的信息可能已经有所发展或是发生改变。

    请教大神,有什么快速的排序方法吗 [{"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 条回复    2019-03-13 14:17:56 +08:00
    myyou
        1
    myyou  
       2019-03-13 14:07:58 +08:00
    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
       2019-03-13 14:14:41 +08:00
    @myyou 牛逼,多谢
    wqzjk393
        3
    wqzjk393  
       2019-03-13 14:17:56 +08:00
    有个比较特别的方法。读进 pandas.dataframe。df = pandas.dataframe(values=dict.value,names=dict.keys) df=df.sorted(by='a'),然后再包装成一个字典返回去
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2828 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 11:31 · PVG 19:31 · LAX 04:31 · JFK 07:31
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.