Python 中有非常方便高效的排序函数,下面主要介绍如何sort/sorted对list,dict进行排序。

1. 用list.sort /sorted 对list of tuples中第二个值进行排序

>>> import operator
>>> a=[('a',3),('b',2),('c',1)]
>>> import operator
>>> l=[('a',3),('b',2),('c',1)]
>>> l.sort(key=operator.itemgetter(1))
>>> l
[('c', 1), ('b', 2), ('a', 3)]
>>> sorted(l, key=operator.itemgetter(1))
[('c', 1), ('b', 2), ('a', 3)]
>>> sorted(l, key=operator.itemgetter(0))
[('a', 3), ('b', 2), ('c', 1)]

  list.sort 和sorted 的区别:sort是list序列的一个方法,而sorted是内建函数

  list.sort: 没有返回值,而且 sort作为序列的内部函数,调用完后会对调用的序列进行排序

  sorted:函数不改变参数,并返回排好序的序列副本

  在python开发文档中对sort和sorted都有详细介绍,也可以调用help函数来查看两者的区别

>>> help(list.sort)
Help on method_descriptor: sort(...)
L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*
>>> help(sorted)
Help on built-in function sorted in module builtins: sorted(iterable, /, *, key=None, reverse=False)
Return a new list containing all items from the iterable in ascending order. A custom key function can be supplied to customize the sort order, and the
reverse flag can be set to request the result in descending order.

2. 除了用operator之外我们也可以用lambda

>>> l.sort(key=lambda x:x[1])
>>> l
[('c', 1), ('b', 2), ('a', 3)]

3. 用sorted来对ditionary进行排序

>>> l = {'a': 3,"b": 2,"c": 1}
>>> sl_key = sorted(l.items()) #Sort by key
>>> sl_key
[('a', 3), ('b', 2), ('c', 1)]
>>> sl_value = sorted(l.items(),key=lambda x:x[1]) #Sort by value
>>> sl_value
[('c', 1), ('b', 2), ('a', 3)]
>>> sl_value = sorted(l.items(),key=lambda x:x[1],
reverse=True) #Sort by value Backwards
>>> sl_value
[('a', 3), ('b', 2), ('c', 1)]
>>> sl_value = sorted(l.items(),key=lambda x:(x[1],x[0]),
reverse=True) #Sort by value then by Key
>>> sl_value
[('a', 3), ('b', 2), ('c', 1)]

  

python学习 -- operator.itemgetter(), list.sort/sorted 以及lambda函数的更多相关文章

  1. Python的operator.itemgetter函数和sorted函数

    写这篇文章的目的是之前在<机器学习实战>用Python3实现KNN算法时用到的几个函数不太懂, 地址: 1- https://github.com/hitergelei/Self-Lear ...

  2. python之map、filter、reduce、lambda函数 转

    python之map.filter.reduce.lambda函数  转  http://www.cnblogs.com/kaituorensheng/p/5300340.html 阅读目录 map ...

  3. python的operator.itemgetter('click')用于定义获取'click'项的函数

    python的排序参见文章http://blog.csdn.net/longshenlmj/article/details/12747195 这里介绍 import operator模块 operat ...

  4. python中operator.itemgetter函数

    operator模块提供的itemgetter函数用于获取对象的哪些维的数据,参数为一些序号(即需要获取的数据在对象中的序号),下面看例子. k = [,,] b = ) print(b(k)) #输 ...

  5. Python学习笔记(八)sorted

    摘抄自:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001431823058 ...

  6. python中operator.itemgetter

    直接上例子: rs=  [...     {...       "datetime": "Mon, 31 Aug 2015 23:00:00 GMT",...  ...

  7. python学习笔记1 -- 函数式编程之高阶函数 sorted排序

    python提供了很强大的内置排序函数,妈妈再也不担心我不会写冒泡排序了呀,sorted函数就是这个排序函数,该函数参数准确的说有四个,sorted(参数1,参数2,参数3,参数4). 参数1 是需要 ...

  8. Python学习笔记之map、zip和filter函数

    这篇文章主要介绍 Python 中几个常用的内置函数,用好这几个函数可以让自己的代码更加 Pythonnic 哦 1.map map() 将函数 func 作用于序列 seq 的每一个元素,并返回处理 ...

  9. Python的函数式编程: map, reduce, sorted, filter, lambda

    Python的函数式编程 摘录: Python对函数式编程提供部分支持.由于Python允许使用变量,因此,Python不是纯函数式编程语言. 函数是Python内建支持的一种封装,我们通过把大段代码 ...

随机推荐

  1. 纯CSS实现内容放大缩小效果

    先搭架子 再实现第一个内容 填充更多内容 拆掉border,查看最终效果 html代码 <!-- 服务 --> <div class="service"> ...

  2. 扒几个 3D 模型备用

    前言 在上一篇中,我展示了 OpenGL 开发的基本过程,算是向 3D 世界迈出的一小步吧.对于简单的 3D 物体,比如立方体.球体.圆环等等,我们只需要简单的计算就可以得到他们的顶点的坐标.但是仅仅 ...

  3. Vue学习笔记-Vue.js-2.X 学习(二)===>组件化开发

    ===重点重点开始 ========================== (三) 组件化开发 1.创建组件构造器: Vue.extends() 2.注册组件: Vue.component() 3.使用 ...

  4. 微信小程序:Navigator导航组件

    导航组件:类似超链接标签. url:要跳转的页面路径,可以放绝对路径,也可以放相对路径,绝对路径指从pages作为根目录开始找到你要的页面. 找到你要找的页面的相对地址的方法:在vscode中,该页面 ...

  5. 链表算法题二,还原题目,用debug调试搞懂每一道题

    文章简述 大家好,本篇是个人的第4篇文章. 承接第3篇文章<开启算法之路,还原题目,用debug调试搞懂每一道题>,本篇文章继续分享关于链表的算法题目. 本篇文章共有5道题目 一,反转链表 ...

  6. JUnit5学习之六:参数化测试(Parameterized Tests)基础

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  7. Python逻辑面试题

    1 # *****************************列表操作***************************** 2 # names = ["Lihua",&q ...

  8. for、while的循环套用和函数的递归

    一.集成开发工具Eclipse 1.1 下载 官网下载 https://www.eclipse.org/downloads/download.php?file=/technology/epp/down ...

  9. MongoDB 在Node中的应用

    转: MongoDB 在Node中的应用 文章目录 一 .什么是 MongoDB? 二.小Demo 三.Demo 增删改查 3.1 新增 3.2 查询 3.2.1 查询所有 [{},{}] 找不到返回 ...

  10. HoloWAN在连接路由器时应该选择WAN口还是LAN口,有什么区别?

    HoloWAN在连接路由器时应该选择WAN口还是LAN口,有什么区别? 在解决问题前,需要连接到,路由器的WAN口和LAN口的作用不同. WAN口是对外的接口,连接广域网.当联网设备和路由器都开启了D ...