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. Puppeteer: 鼠标移动

    文档 mouse.click 是 mouse.move,mouse.down 和 mouse.up 的快捷方式 main.js const pptr = require('puppeteer'); c ...

  2. 1102 Invert a Binary Tree——PAT甲级真题

    1102 Invert a Binary Tree The following is from Max Howell @twitter: Google: 90% of our engineers us ...

  3. synchronized语法

    synchronized( ){ } synchronized 关键字是加锁的意思,用它来修饰方法就表示给该方法加了锁,从而达到线程同步的效果;用它来修饰代码块就表示给该代码块加了锁,从而达到线程同步 ...

  4. 鸿蒙开源第三方组件 ——B站开源弹幕库引擎的迁移(上)

    鸿蒙入门指南,小白速来!0基础学习路线分享,高效学习方法,重点答疑解惑--->[课程入口] 目录: 一.弹幕库的基础知识 二.弹幕库的使用方法 三.sample解析 四.作者系列文章合集 前言 ...

  5. HTML:HTML基础

    HTML不是一门编程语言,而是一种用于定义内容结构的标记语言.HTML由一系列元素(elements)组成,这些元素可以用来包围不同部分的内容,使其以某种方式呈现或工作.一对标签可以为一段文字或者一张 ...

  6. (三)String、StringBuilder、StringBuffer在字符串操作中的性能差异浅析

    参考资料:https://www.iteye.com/blog/hank4ever-581463 <Core Java Volume I-Fundamentals>原书第十版 <Ja ...

  7. .NET Core Generic Host项目使用Topshelf部署为Windows服务

    1..NET Core Generic Host是什么? 在.NET Core 2.1版本加入了一种新的Host,即Generic Host(通用主机). 现在在2.1版本的Asp.Net Core中 ...

  8. $nextTick解决Vue组件卸载在加载合并的问题

    情况是这样的,父子组件都是复选框,点击父组件的复选框,子组件的复选框要显示并全选,取消复选框,子组件隐藏.子组件显隐我用的 v-if ,使用created钩子函数来使子组件处于全选状态. 但是出现的问 ...

  9. C#的foreach遍历循环和隐式类型变量

    C#的foreach遍历循环和隐式类型变量 foreach遍历循环 foreach (<baseType> <name> in <array>>) { //c ...

  10. 通达OA 页面敏感信息-2013/2015版本

    参考 http://wiki.0-sec.org/0day/%E9%80%9A%E8%BE%BEoa/4.html 漏洞影响 2013.2015版本 复现过程 POC: http://0-sec.or ...