python笔记18-sort和sorted区别
前言
python的排序有两个方法,一个是list对象的sort方法,另外一个是builtin函数里面sorted,主要区别:
- sort仅针对于list对象排序,无返回值, 会改变原来队列顺序
- sorted是一个单独函数,可以对可迭代(iteration)对象排序,不局限于list,它不改变原生数据,重新生成一个新的队列
本篇是基于python3.6讲解的,python2会多一个cmp参数,cmp函数在python3上已经丢弃了
cmp(x,y) 函数用于比较2个对象,如果 x < y 返回 -1, 如果 x == y 返回 0, 如果 x > y 返回 1。
sort方法
1.sort是list对象的方法,通过.sort()来调用
>>> help(list.sort)
Help on method_descriptor:
sort(...)
L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*
>>>
2.参数说明:
key 用列表元素的某个属性或函数进行作为关键字(此函数只能有一个参数)
reverse 排序规则. reverse = True 降序 或者 reverse = False 升序,默认升序
return 无返回值
3.使用方法介绍
# coding:utf-8
a = [-9, 2, 3, -4, 5, 6, 6, 1]
# 按从小到大排序
a.sort()
print(a) # 结果:[-9, -4, 1, 2, 3, 5, 6, 6]
# 按从大到小排序
a.sort(reverse=True)
print(a) # 结果:[6, 6, 5, 3, 2, 1, -4, -9]
4.key参数接受的是函数对象,并且函数只能有一个参数,可以自己定义一个函数,也可以写个匿名函数(lambda)
# coding:utf-8
a = [-9, 2, 3, -4, 5, 6, 6, 1]
# 按绝对值排序
def f(x):
return abs(x)
a.sort(key=f)
print(a) # 结果:[1, 2, 3, -4, 5, 6, 6, -9]
# 1、list对象是字符串
b = ["hello", "helloworld", "he", "hao", "good"]
# 按list里面单词长度倒叙
b.sort(key=lambda x: len(x), reverse=True)
print(b) # 结果:['helloworld', 'hello', 'good', 'hao', 'he']
# 2、.list对象是元组
c = [("a", 9), ("b", 2), ("d", 5)]
# 按元组里面第二个数排序
c.sort(key=lambda x: x[1])
print(c) # 结果:[('b', 2), ('d', 5), ('a', 9)]
# 3、list对象是字典
d = [{"a": 9}, {"b": 2}, {"d":5}]
d.sort(key=lambda x: list(x.values())[0])
print(d) # 结果:[{'b': 2}, {'d': 5}, {'a': 9}]
sorted函数
1.sorted是python里面的一个内建函数,直接调用就行了
>>> 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.参数说明
iterable 可迭代对象,如:str、list、tuple、dict都是可迭代对象(这里就不局限于list了)
key 用列表元素的某个属性或函数进行作为关键字(此函数只能有一个参数)
reverse 排序规则. reverse = True 降序或者 reverse = False 升序,默认升序
return 有返回值值,返回新的队列
3.使用方法介绍
# coding:utf-8
a = [-9, 2, 3, -4, 5, 6, 6, 1]
# 按从小到大排序
b = sorted(a)
print(a) # a不会变
print(b) # b是新的队列 [-9, -4, 1, 2, 3, 5, 6, 6]
# 按从大到小排序
c = sorted(a, reverse=True)
print(c) # 结果:[6, 6, 5, 3, 2, 1, -4, -9]
4.可迭代对象iterable都可以排序,返回结果会重新生成一个list
# coding:utf-8
# 字符串也可以排序
s = "hello world!"
d = sorted(s)
print(d) # 结果:[' ', '!', 'd', 'e', 'h', 'l', 'l', 'l', 'o', 'o', 'r', 'w']
# 元组也可以排序
t = (-9, 2, 7, 3, 5)
n = sorted(t)
print(n) # 结果:[-9, 2, 3, 5, 7]
# dict按value排序
f = {"a": 9, "b": 2, "d": 5}
g = sorted(f.items(), key=lambda x: x[1])
print(g) # 结果:[('b', 2), ('d', 5), ('a', 9)]
python自动化交流 QQ群:779429633
python笔记18-sort和sorted区别的更多相关文章
- python排序函数sort()与sorted()区别
sort是容器的函数:sort(cmp=None, key=None, reverse=False) sorted是python的内建函数:sorted(iterable, cmp=None, key ...
- sort 与 sorted 区别
sort 与 sorted 区别: sort 只是应用在 list 上的方法,(就地排序无返回值). sorted 是内建函数,可对所有可迭代的对象进行排序操作,(返回新的list). 语法 sort ...
- sort 与 sorted 区别:
sort 与 sorted 区别: sort 是应用在 list 上的方法,sorted 可以对所有可迭代的对象进行排序操作. list 的 sort 方法返回的是对已经存在的列表进行操作,无返回值, ...
- python中list.sort()与sorted()的区别
list.sort()和sorted()都是python的内置函数,他们都用来对序列进行排序,区别在于 list.sort()是对列表就地(in-place)排序,返回None:sorted()返回排 ...
- Python中的 sort 和 sorted
今天在做一道题时,因为忘了Python中sort和sorted的用法与区别导致程序一直报错,找了好久才知道是使用方法错误的问题!现在就大致的归纳一下sort和sorted的用法与区别 1. sort: ...
- python中的sort、sorted、reverse、reversed详解
python语言中的列表排序方法有三个:reverse反转/倒序排序.sort正序排序.sorted可以获取排序后的列表.在更高级列表排序中,后两中方法还可以加入条件参数进行排序. reverse() ...
- python中sort与sorted区别
1.sort()函数 (只对list有用) sort(...) L.sort(key = None,reverse=False) key = 函数 这个函数会从每个元素中提取一个用于比较的关键字.默认 ...
- python sort和sorted区别。
前者是方法,后者是函数.oop和opp区别的经典体现.好好领会,就能知道什么时候写类什么时候写函数好.
- python中的sort和sorted
共同点 都有三个参数, cmp用户自定义(指定函数),每个元素都会调用,效率没key高 key带一个参数的函数,用来为每个元素提取比较值 reverse=True 翻转 sort sort作用的 ...
- python list列表sort、sorted、reverse排序
列表排序:sort是修改原列表,sorted提供原列表的一个有序副本 li=[2,1,4,5,0]li.sort() #默认从小到大print li结果:[0, 1, 2, 4, 5] li=[2,1 ...
随机推荐
- java 基础知识-数组的7种算法(排序、求和、最值、遍历...)
遍历 遍历就是把这个数组的每个元素 显示出来 遍历的方法就是先定义这个数组的大小,然后用FOR循环来完成数组,例如 double[] score = new double[5]; Scanner in ...
- ceph笔记(一)
一.ceph概述本质上是rados:可靠的.自动的.分布式对象存储特性:高效性(大型的网络raid,性能无限接近raid).统一性(支持文件存储.块存储.对象存储).可扩展性数据库的一个弱点:查表ce ...
- jquery 美化弹出提示 漂亮的Dialog 对话框
三个不同的效果,分别是普通的警告,确认/取消,带一个输入框 本例用了jquery.alertify.js,请到演示页面查看 css文件也请到演示页面查看 JavaScript Code <scr ...
- U3D 基础
千里之行,始于足下! 最先执行的方法是:1.(激活时的初始代码)Awake2.Start3.Update(FixUpdate,LateUpdate)4.渲染模块(OnGUI)5.再向后,就是卸载模块( ...
- php实现var_dump函数
<?php class VarDump { private static $isInLoop = false; private static $buffer = false; public st ...
- DP 题集 2
关于 DP 的一些题目 String painter 先区间 DP,\(dp[l][r]\) 表示把一个空串涂成 \(t[l,r]\) 这个子串的最小花费.再考虑 \(s\) 字符串,\(f[i]\) ...
- 最短路 之 floyd 算法
Floyd 在我认为这是最短路算法中最简单的一个,也是最low的一个. 所以我们组一位大佬给他起了一个新的名字,叫做超时!!! (其实如果数据范围很小的话,这个算法还是蛮好用的!!) 这个算法比较简单 ...
- Web后门工具WeBaCoo
Web后门工具WeBaCoo WeBaCoo是使用Perl语言编写的Web后门工具.渗透测试人员首先使用该工具生成一个后门PHP页面.然后,将该页面上传到目标服务器上.最后,在本地终端直接访问该页 ...
- django celery(QQ蓝鲸)
官方:http://www.celeryproject.org 文档:http://docs.jinkan.org/docs/celery/index.html FAQ:http://docs.cel ...
- 【UOJ #205】【APIO 2016】Fireworks
http://uoj.ac/problem/205 好神的题啊. dp[i][j]表示以i为根的子树调整成长度j需要的最小代价. 首先要观察到dp值是一个下凸壳. 因为从儿子合并到父亲时要把所有儿子的 ...