[习题] 对此字典分别按照value 和key 如何排序?

dic1 = {'and':40, 'a':54, 'is':60, 'path':139, 'the':124, 'os':49}

In [38]: dic1 = {'and':40, 'a':54, 'is':60, 'path':139, 'the':124, 'os':49}
...: print(sorted(zip(dic1.values(),dic1.keys()))) #按value的值升序
...: print(sorted(zip(dic1.values(),dic1.keys()),reverse=True)) #按value的值降序
...:
...: print(sorted(zip(dic1.keys(),dic1.values()))) #按key的ascii码升序
...: print(sorted(zip(dic1.keys(),dic1.values()),reverse=True)) #按key的ascii码降序
...:
...: print(sorted(dic1.items(),key=lambda x:x[1],)) #按value的值升序
...: print(sorted(dic1.items(),key=lambda x:x[1],reverse=True)) #按value的值降序
...:
...: print(sorted(dic1.items(),key=lambda x:x[0],)) #按key的ascii码升序
...: print(sorted(dic1.items(),key=lambda x:x[0],reverse=True)) #按key的ascii码降序
...:
[(40, 'and'), (49, 'os'), (54, 'a'), (60, 'is'), (124, 'the'), (139, 'path')]
[(139, 'path'), (124, 'the'), (60, 'is'), (54, 'a'), (49, 'os'), (40, 'and')] [('a', 54), ('and', 40), ('is', 60), ('os', 49), ('path', 139), ('the', 124)]
[('the', 124), ('path', 139), ('os', 49), ('is', 60), ('and', 40), ('a', 54)] [('and', 40), ('os', 49), ('a', 54), ('is', 60), ('the', 124), ('path', 139)]
[('path', 139), ('the', 124), ('is', 60), ('a', 54), ('os', 49), ('and', 40)] [('a', 54), ('and', 40), ('is', 60), ('os', 49), ('path', 139), ('the', 124)]
[('the', 124), ('path', 139), ('os', 49), ('is', 60), ('and', 40), ('a', 54)]

  

知识点: zip(),sorted(),lambda

zip: 

zip函数接受任意多个(包括0个和1个)序列作为参数,返回一个tuple列表。

用法: zip(iter1 [,iter2 [...]]) --> zip object:

In [79]: a = 'abcdefg'

In [80]: b = '1234567890'

In [81]: zip(a,b)
Out[81]: <zip at 0x1ce2b8d4fc8> In [82]: c = zip(a,b) In [83]: for i in c:
...: print(i)
...:
('a', '1')
('b', '2')
('c', '3')
('d', '4')
('e', '5')
('f', '6')
('g', '7')

sorted:

对指定可迭代对象iterable 排序并返回一个新对象,不改变原数据;key 可以设置为按str、int或者指定值(如字典的value)排序,默认是None,将按照默认对象类型排序,如果对象是str,则按ascii 码排序,如果是对象是int 数字,则按数字排序;reverse 默认升序(False),True 为降序。

sorted(iterable, key=None, reverse=False)

lambda:

最后两处lambda 比较绕,可以在( http://pythontutor.com/visualize.html#mode=display )这个网站边调试边分析x 和x[1] 的值是什么。

以下截图中,lambda表达式中x的值是一个tuple("and",40),x[1] 就表示第1个元素(40),最终sorted 就是按字典的value 来进行排序。

Python [习题] 字典排序的更多相关文章

  1. 深入Python(1): 字典排序 关于sort()、reversed()、sorted()

    http://www.cnblogs.com/BeginMan/p/3193081.html 一.Python的排序 1.reversed() 这个很好理解,reversed英文意思就是:adj. 颠 ...

  2. python中字典排序,列表中的字典排序

    python中字典排序,列表中的字典排序 一.使用python模块:operator import operator #首先要导入模块operator x = {1:2, 3:4, 4:3, 2:1, ...

  3. python中字典排序

    一.Python的排序 1.reversed() 这个很好理解,reversed英文意思就是:adj. 颠倒的:相反的:(判决等)撤销的 print list(reversed(['dream','a ...

  4. python进阶--字典排序

    zip()函数 sorted() 要求对字典中,按值的大小排序 解决方案: 利用zip函数 zip函数介绍: zip函数可以将可迭代对象打包成一个个元组,在python3中返回一个对象,在python ...

  5. python 对字典"排序"

    对字典进行排序?这其实是一个伪命题,搞清楚python字典的定义---字典本身默认以key的字符顺序输出显示---就像我们用的真实的字典一样,按照abcd字母的顺序排列,并且本质上各自没有先后关系,是 ...

  6. Python - DICT 字典排序 - OrderedDict

    官方地址: https://docs.python.org/2/library/collections.html#collections.OrderedDict >>> # regu ...

  7. Python [习题] 字典扁平化

    习题: 将以下字典扁平化,输出为 target 字典格式source = {'a': {'b': 1, 'c': 2}, 'd': {'e': 3, 'f': {'g': 4}}}target = { ...

  8. python给字典排序

    应用场景: 统计一篇文章中单词的出现频率,然后进行排序 利用sorted函数,返回一个已经排序好的list,但不改变原来的数据结构 In [1]: dt = {'a':3,'b':2,'c':1} I ...

  9. [Python] dict字典排序和多条件排序

    利用lambda实现排序:要实现多条件排序,只需要依次指定排序的标准,具体实现如下 counter = {'是': 1, '不是': 1, '你': 3} counter_list = sorted( ...

随机推荐

  1. Linux命令-基本命令(1)

    1. ll dfdfdfd 2. vi dfffd

  2. 多个 (li) 标签如何获取获取选中的里面的某个特定值??

    两种方式: 1/.根据div中的class属性 指定ul 找到选中的单个li $(".f_dingdan ul li").click(function(){    var a=$( ...

  3. Java 7 JVM和垃圾收集

    ---恢复内容开始--- 写JAVA程序,一定要了解JVM(JAVA Virtual machine)一些基础知识和垃圾收集.如果对JVM已经很了解了,可以不用继续往下阅读了.本文只针对Java 7, ...

  4. AVL 树

    一棵AVL树是每个节点的左子树和右子树的高度最多差1的二叉查找树 SearchTree Insert(ElementType X, SearchTree T) { if (T == NULL) { T ...

  5. 开源API集成测试工具 Hitchhiker v0.3更新 - 自动同步

    Hitchhiker 是一款开源的 Restful Api 集成测试工具,支持Schedule, 数据对比,压力测试,可以轻松部署到本地,和你的team成员一起管理Api. 详细介绍请看: http: ...

  6. Oracle 表空间扩充

    Oracle 表空间扩充 一.现场环境: (1)操作系统:AIX (2)数据库:Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - ...

  7. I - Intersection HDU - 5120(圆环相交面积)

    Matt is a big fan of logo design. Recently he falls in love with logo made up by rings. The followin ...

  8. 暑假练习赛 006 E Vanya and Label(数学)

    Vanya and LabelCrawling in process... Crawling failed Time Limit:1000MS     Memory Limit:262144KB    ...

  9. Day1作业要求

    Day1作业 作业需求 博客 模拟登录 三级菜单 博客地址 杨振伟Day1博客地址 模拟登录 1.程序说明 实现功能如下 用户输入密码,密码验证后登录成功 用户登录成功后提示登录信息 用户输入3次错误 ...

  10. Windows下caffe的配置和调用caffe库(一)

    一.Windows下caffe的配置: 1. 下载caffe官网提供的开发包,https://github.com/microsoft/caffe 2. 将caffe-master目录下的Window ...