• python 中 实现按照字典的键和值进行排序


    001、

    >>> dict1 = {"d":400, "a":300, "e":500, "b":700, "c":600}         ## 测试字典
    >>> dict1
    {'d': 400, 'a': 300, 'e': 500, 'b': 700, 'c': 600}
    >>> sorted(dict1.keys())                                          ## 对字典的键进行排序
    ['a', 'b', 'c', 'd', 'e']
    >>> sorted(dict1.values())                                        ## 对字段的值进行排序
    [300, 400, 500, 600, 700]

    002、返回元组

    >>> dict1 = {"d":400, "a":300, "e":500, "b":700, "c":600}          ## 测试字典
    >>> dict1
    {'d': 400, 'a': 300, 'e': 500, 'b': 700, 'c': 600}
    >>> sorted(dict1.items(), key = lambda x: x[0])                    ## 依据字典的键,对项进行排序
    [('a', 300), ('b', 700), ('c', 600), ('d', 400), ('e', 500)]
    >>> sorted(dict1.items(), key = lambda x: x[1])                    ## 依据字典的值,对项进行排序
    [('a', 300), ('d', 400), ('e', 500), ('c', 600), ('b', 700)]
    >>> sorted(dict1.items(), key = lambda x: x[0], reverse = True)    ## 增加reverse = True; 逆向排序
    [('e', 500), ('d', 400), ('c', 600), ('b', 700), ('a', 300)]
    >>> sorted(dict1.items(), key = lambda x: x[1], reverse = True)
    [('b', 700), ('c', 600), ('e', 500), ('d', 400), ('a', 300)]
    >>> dict1 = {"d":400, "a":300, "e":500, "b":700, "c":600}
    >>> dict1
    {'d': 400, 'a': 300, 'e': 500, 'b': 700, 'c': 600}
    >>> sorted(dict1.items(), key = lambda x: x[0])
    [('a', 300), ('b', 700), ('c', 600), ('d', 400), ('e', 500)]
    >>> dict(sorted(dict1.items(), key = lambda x: x[0]))              ## 依据字典的键进行排序, 并返回字典
    {'a': 300, 'b': 700, 'c': 600, 'd': 400, 'e': 500}
    >>> sorted(dict1.items(), key = lambda x: x[1])
    [('a', 300), ('d', 400), ('e', 500), ('c', 600), ('b', 700)]
    >>> dict(sorted(dict1.items(), key = lambda x: x[1]))              ## 依据字典的值进行排序, 并返回字典
    {'a': 300, 'd': 400, 'e': 500, 'c': 600, 'b': 700}

    003、

    >>> dict1 = {"d":400, "a":300, "e":500, "b":700, "c":600}   
    >>> dict2 = {}
    >>> for i in sorted(dict1):                                    依据键进行排序
    ...     dict2[i] = dict1[i]
    ...
    >>> dict2
    {'a': 300, 'b': 700, 'c': 600, 'd': 400, 'e': 500}

    004、借助于import operator 包

    >>> dict1 = {"d":400, "a":300, "e":500, "b":700, "c":600}          ## 测试字典
    >>> dict1
    {'d': 400, 'a': 300, 'e': 500, 'b': 700, 'c': 600}
    >>> import operator                                                ## 导入包
    >>> dict(sorted(dict1.items(), key=operator.itemgetter(0)))        ## 依据字典的键进行排序
    {'a': 300, 'b': 700, 'c': 600, 'd': 400, 'e': 500}
    >>> dict(sorted(dict1.items(), key=operator.itemgetter(1)))        ## 依据字典的值进行排序
    {'a': 300, 'd': 400, 'e': 500, 'c': 600, 'b': 700}
  • 相关阅读:
    Java实现监控目录下文件变化
    Postgresql 修改用户密码
    Swing清空jtable中的数据
    delphi登录用友的信息
    用友U8的SQL SERVER 数据库结构说明表
    候老师的讲堂:视频录制、笔记软件、思维导图、画图等工具
    DELPHI 关于内存数据与 JSON
    Delphi国内优秀网站及开源项目
    SQL Server 阻止了对组件Ad Hoc Distributed Queries访问的方法
    SQL Server跨服务器查询
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/16581247.html
Copyright © 2020-2023  润新知