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

一个关于排序的问题,请各位大佬赐教

  •  
  •   duyuyouci · 2020-08-19 16:41:41 +08:00 · 2042 次点击
    这是一个创建于 1547 天前的主题,其中的信息可能已经有所发展或是发生改变。
    data_list = [{1: "1"}, {3: "3"}, {4: "4"}, {10: "10"}, {7: "7"}, {6: "6"}],
    类似这样的数据,怎么用 sort 去排序,sort 里的 key 关键字参数要怎么写
    16 条回复    2020-08-22 16:03:21 +08:00
    Akikiki
        1
    Akikiki  
       2020-08-19 16:48:37 +08:00
    data_list.sort(key=lambda x: x.keys()[0])
    duyuyouci
        2
    duyuyouci  
    OP
       2020-08-19 16:53:05 +08:00
    @Akikiki 会报错的,TypeError: 'dict_keys' object is not subscriptable
    h272377502
        3
    h272377502  
       2020-08-19 16:54:00 +08:00
    就假设你的字典都是一个 key,sorted(data_list, key=lambda k: list(k.keys())[0])
    duyuyouci
        4
    duyuyouci  
    OP
       2020-08-19 16:58:15 +08:00
    @h272377502 对呀,转化一下类型就好了,厉害
    Akikiki
        5
    Akikiki  
       2020-08-19 17:04:19 +08:00
    @duyuyouci 哦 你是 python3 吧
    mahonejolla
        6
    mahonejolla  
       2020-08-19 17:05:37 +08:00
    data_list = [{1: "1"}, {3: "3"}, {4: "4"}, {10: "10"}, {7: "7"}, {6: "6"}]
    kk = data_list.sort(key=lambda x: list(x.keys())[0])
    print(data_list) # [{1: '1'}, {3: '3'}, {4: '4'}, {6: '6'}, {7: '7'}, {10: '10'}]
    kk = sorted(data_list, key=lambda k: list(k.keys())[0])
    print(kk) # [{1: '1'}, {3: '3'}, {4: '4'}, {6: '6'}, {7: '7'}, {10: '10'}]
    lithbitren
        7
    lithbitren  
       2020-08-21 09:21:50 +08:00
    data_list.sort(key=lambda x: next(iter(x)))

    不转 list 也可以实现
    yucongo
        8
    yucongo  
       2020-08-21 14:57:38 +08:00 via Android
    sorted(data_list, key=lambda x: [*x])
    duyuyouci
        9
    duyuyouci  
    OP
       2020-08-21 16:42:37 +08:00
    @yucongo 这个好像不行,顺序没有变
    duyuyouci
        10
    duyuyouci  
    OP
       2020-08-21 16:42:45 +08:00
    @lithbitren 高级
    yucongo
        11
    yucongo  
       2020-08-21 19:31:49 +08:00
    In [43]: data_list = [{1: "1"}, {3: "3"}, {4: "4"}, {10: "10"}, {7: "7"}, {6: "6"}]

    In [44]: sorted(data_list, key=lambda x: [*x])
    Out[44]: [{1: '1'}, {3: '3'}, {4: '4'}, {6: '6'}, {7: '7'}, {10: '10'}]

    完全用你的数据,python3.6, 顺序怎么没有变呢
    yucongo
        12
    yucongo  
       2020-08-21 19:40:57 +08:00
    或( in-place ):
    data_list.sort(key=lambda x: [*x])
    duyuyouci
        13
    duyuyouci  
    OP
       2020-08-22 09:44:33 +08:00
    @yucongo 哦,sorted 是创建了一个副本,我打印的原列表,哈哈,高级,但是这个语法不太懂,老哥能解释吗
    lithbitren
        14
    lithbitren  
       2020-08-22 10:53:02 +08:00
    @duyuyouci

    [*x]相当于[i for i in x],也相当于 list(x)

    他这个一行其实可以直接写成 data_list.sort(key=list),本质还是转数组
    duyuyouci
        15
    duyuyouci  
    OP
       2020-08-22 11:48:36 +08:00
    @lithbitren 原来如此,受教了
    yucongo
        16
    yucongo  
       2020-08-22 16:03:21 +08:00
    对啊,key=list 就行了……
    那么可以来一个最短的:)
    sorted(data_list, key=set)

    data_list.sort(key=set)
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5840 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 02:55 · PVG 10:55 · LAX 18:55 · JFK 21:55
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.