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 ...
随机推荐
- csu 1556(快速幂)
1556: Jerry's trouble Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 787 Solved: 317[Submit][Statu ...
- hdu 5839(三维几何)
Special Tetrahedron Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- [loj6039]「雅礼集训 2017 Day5」珠宝 dp+决策单调性+分治
https://loj.ac/problem/6039 我们设dp[i][j]表示考虑所有价值小于等于i的物品,带了j块钱的最大吸引力. 对于ci相同的物品,我们一定是从大到小选k个物品,又发现最大的 ...
- bzoj 1833 数位dp
很裸的数位dp. #include<bits/stdc++.h> #define LL long long #define fi first #define se second #defi ...
- cent 7.0 安装mysql
安装命令 wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm rpm -ivh mysql-community ...
- C++ 字符串基本操作
C++ 规定,不能直接进行数组名的赋值,因为数组名是一个常量,而结构类型的变量可以赋值,不同结构体的变量不允许相互赋值,即使这两个变量可能具有相同的成员.在程序中不能同时出现无参构造函数和带有全部默认 ...
- 【WPF】生成二维码
第一步,下载Google的ZXing类库,以便引用: BitMatrix bitMatrix; private void Button_Click_1(object sender, RoutedEve ...
- HDU 5707 Combine String(动态规划)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5707 题意: 给你三个字符串 S1, S2, S3, 让你判断 S3 是否恰好由字符串 S1 和 S2 ...
- EOJ 3262 黑心啤酒厂
最大公约数. 计算$x$与$i$的最小公倍数,就是这些人至少需要喝几杯,最小公倍数除以$x$就是要买几瓶. #include <cstdio> #include <cmath> ...
- [转]SharePoint 2010 Powershell Feature Cmdlets
In this installment its time to look at the various cmdlets that have to do with Features. Of course ...