Python中的sorted函数
今天在做一个中文文本分类的项目,遇到了一个sorted函数,发现并不会用。。。
记录一下:
sorted(list, key, reverse)
list是给定的列表;
key是排序过程调用的函数,也就是排序依据
reverse是降序还是升序,默认为False升序,True降序,
举几个例子:
1、按照列表中的每个值的绝对值进行排序
l1 = [1,3,5,-2,-4,-6]
l2 = sorted(l1,key=abs)
print(l1)
print(l2) #
[1, 3, 5, -2, -4, -6]
[1, -2, 3, -4, 5, -6]
2、按照列表中每个元素长度进行排序
l = [[1,2],[3,4,5,6],(7,),'']
print(sorted(l,key=len)) #
[(7,), [1, 2], '', [3, 4, 5, 6]]
3、按照词频大小进行降序
word_dict = {'apple':20, 'love':15}
sorted_word_dict = sorted(word_dict.items(), key=lambda d:d[1])
print(sorted_word_dict)
#
[('love', 15), ('apple', 20)]
Python中的sorted函数的更多相关文章
- Python中的sorted函数以及operator.itemgetter函数 【转载】
operator.itemgetter函数operator模块提供的itemgetter函数用于获取对象的哪些维的数据,参数为一些序号(即需要获取的数据在对象中的序号),下面看例子. a = [1,2 ...
- Python中的sorted函数以及operator.itemgetter函数
operator.itemgetter函数operator模块提供的itemgetter函数用于获取对象的哪些维的数据,参数为一些序号(即需要获取的数据在对象中的序号),下面看例子. a = [1,2 ...
- python --- Python中的callable 函数
python --- Python中的callable 函数 转自: http://archive.cnblogs.com/a/1798319/ Python中的callable 函数 callabl ...
- python中使用zip函数出现<zip object at 0x02A9E418>
在Python中使用zip函数,出现<zip object at 0x02A9E418>错误的原因是,你是用的是python2点多的版本,python3.0对python做了改动 zip方 ...
- [转载]python中multiprocessing.pool函数介绍
原文地址:http://blog.sina.com.cn/s/blog_5fa432b40101kwpi.html 作者:龙峰 摘自:http://hi.baidu.com/xjtukanif/blo ...
- Python 中的isinstance函数
解释: Python 中的isinstance函数,isinstance是Python中的一个内建函数 语法: isinstance(object, classinfo) 如果参数object是cla ...
- Python中的map()函数和reduce()函数的用法
Python中的map()函数和reduce()函数的用法 这篇文章主要介绍了Python中的map()函数和reduce()函数的用法,代码基于Python2.x版本,需要的朋友可以参考下 Py ...
- python中multiprocessing.pool函数介绍_正在拉磨_新浪博客
python中multiprocessing.pool函数介绍_正在拉磨_新浪博客 python中multiprocessing.pool函数介绍 (2010-06-10 03:46:5 ...
- 举例详解Python中的split()函数的使用方法
这篇文章主要介绍了举例详解Python中的split()函数的使用方法,split()函数的使用是Python学习当中的基础知识,通常用于将字符串切片并转换为列表,需要的朋友可以参考下 函数:sp ...
随机推荐
- mysql批量替换zencart数据中的反斜杠\
update products_description set products_description=replace(products_description, "\\'",& ...
- htmlunit与Jsoup
//这个函数的目的是在获取页面的同时,也获取链接对应的cookiepublic static HtmlPage getCookieAndHtml(String url)throws IOExcepti ...
- pyqt-swf
# pyqt5界面打开flash.swf文件 from PyQt5 import QtCore, QtGui, QAxContainer, QtWidgets class Ui_Flash(QAxCo ...
- redis 日志等级说明
redis loglevel 安装默认的设置为 verbose 1)debug:会打印出很多信息,适用于开发和测试阶段 2)verbose(冗长的):包含很多不太有用的信息,但比debug要清晰一些 ...
- vue学习-day01(vue指令)
目录: 1.什么是vue.js 2.为什么要学习前端的流行框架 3.框架和库的区别 4.后端MVC和前端的MVVM的区别 5.vue.js的基本代码--hollo world代 ...
- 《数据结构(C语言)》苏小红 课本案例
期末了,赶紧复习一波,手打一份书上的代码以便随时查阅 第二章: //顺序表存储结构 #define MAXSIZE 100 typedef struct { Elemtype *elemt; int ...
- 前端面试题-CSS选择器
一.CSS选择器作用 CSS 选择器用于定位我们想要给予样式的 HTML 元素,但不只是在 CSS 中,JavaScript 对 CSS 的选择器也是支持的,比如 document.document. ...
- rsync aws ec2 pem
How to use aws ec2 pem http://www.anthonychambers.co.uk/blog/rsync-to-aws-ec2-using-.pem-key/9 方法如下: ...
- java中的同步和异步
摘自:https://www.cnblogs.com/caotao0918/p/10699785.html 在多线程的环境中,经常会碰到数据的共享问题,即当多个线程需要访问同一个资源时,它们需要以某种 ...
- Mac securecrt 破解版安装
破解一 1.先链接:https://pan.baidu.com/s/1-1nu4eRf7BmuLg5MtlCRvw 密码:30pq 默认下载到了当前用户的”下载”目录中 在”Finder”中 ...