python学习 -- operator.itemgetter(), list.sort/sorted 以及lambda函数
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函数的更多相关文章
- Python的operator.itemgetter函数和sorted函数
写这篇文章的目的是之前在<机器学习实战>用Python3实现KNN算法时用到的几个函数不太懂, 地址: 1- https://github.com/hitergelei/Self-Lear ...
- python之map、filter、reduce、lambda函数 转
python之map.filter.reduce.lambda函数 转 http://www.cnblogs.com/kaituorensheng/p/5300340.html 阅读目录 map ...
- python的operator.itemgetter('click')用于定义获取'click'项的函数
python的排序参见文章http://blog.csdn.net/longshenlmj/article/details/12747195 这里介绍 import operator模块 operat ...
- python中operator.itemgetter函数
operator模块提供的itemgetter函数用于获取对象的哪些维的数据,参数为一些序号(即需要获取的数据在对象中的序号),下面看例子. k = [,,] b = ) print(b(k)) #输 ...
- Python学习笔记(八)sorted
摘抄自:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001431823058 ...
- python中operator.itemgetter
直接上例子: rs= [... {... "datetime": "Mon, 31 Aug 2015 23:00:00 GMT",... ...
- python学习笔记1 -- 函数式编程之高阶函数 sorted排序
python提供了很强大的内置排序函数,妈妈再也不担心我不会写冒泡排序了呀,sorted函数就是这个排序函数,该函数参数准确的说有四个,sorted(参数1,参数2,参数3,参数4). 参数1 是需要 ...
- Python学习笔记之map、zip和filter函数
这篇文章主要介绍 Python 中几个常用的内置函数,用好这几个函数可以让自己的代码更加 Pythonnic 哦 1.map map() 将函数 func 作用于序列 seq 的每一个元素,并返回处理 ...
- Python的函数式编程: map, reduce, sorted, filter, lambda
Python的函数式编程 摘录: Python对函数式编程提供部分支持.由于Python允许使用变量,因此,Python不是纯函数式编程语言. 函数是Python内建支持的一种封装,我们通过把大段代码 ...
随机推荐
- Puppeteer: 虚拟键盘
文档 main.js const pptr = require('puppeteer'); const gotoUrl = 'http://127.0.0.1:5500/index.html'; (a ...
- Android Studio 3.3.1 向avd模拟器发送本地文件
"工具栏/View/Tool Windows/Device File Pxplorer" 选择模拟器在找到对应的文件夹upload即可
- JULLIAN MURPHY:拥有良好的心态,运气福气便会自来
JULLIAN MURPHY是星盟全球投资公司的基金预审经理,负责星盟投资项目预审,有着资深的基金管理经验,并且在区块链应用的兴起中投资了多个应用区块链技术的公司. JULLIAN MURPHY认为往 ...
- 教你玩转CSS border(边框)
边框样式 边框样式属性指定要显示什么样的边界. border-style属性用来定义边框的样式 border-style的值 代码演示: <!DOCTYPE html> <html ...
- 用Python实现一个“百度翻译”
import requests import json s = input("请输入你要翻译的内容:") headers = {"User-Agent":&qu ...
- Flex实现左右布局
html <div class="business-content-1"> <div class="item"> 111 </di ...
- Newbe.Claptrap 框架入门,第一步 —— 开发环境准备
Newbe.Claptrap 框架依托于一些关键性的基础组件和一些可选的辅助组件.本篇我们来介绍一下如何准备一个开发环境. Newbe.Claptrap 是一个用于轻松应对并发问题的分布式开发框架.如 ...
- 注册 Amazon Web Services(AWS) 账号,助园一臂之力
感谢大家去年的大力支持,今年园子继续和 Amazon Web Services(AWS) 合作,只要您通过 博客园专属链接 注册一个账号(建议使用手机4G网络注册),亚马逊就会给园子收入,期待您的支持 ...
- Linux graphics stack
2D图形架构 早期Linux图形系统的显示全部依赖X Server,X Client调用Xlib提供的借口向 X Server发送渲染命令,X Server根据 X Client的命令请求向硬件设备绘 ...
- GDB调试:从入门到入土
GDB是类Unix操作糸统下使用命令行调试的调试软件,全名GNU Debugger,在NOI系列竞赛使用的NOI Linux系统中起很大作用(如果不想用毒瘤Guide或直接输出)(XXX为文件名) 1 ...