Python中sort 、sorted、argsort的区别
来源:互联网 发布:淘宝秒杀抢购 编辑:程序博客网 时间:2024/06/06 00:33
1、sort函数是list列表中的函数
>>a = [1,2,1,4,3,5]
>>a.sort()
>>a 输出[1, 1, 2, 3, 4, 5]
2、sorted可以对list或者iterator进行排序
>>a = [1,2,1,4,3,5]
>>sorted(a) 输出[1, 1, 2, 3, 4, 5]
>>a 输出[1, 2, 1, 4, 3, 5]
Ps:用sort函数对列表排序时会影响列表本身,而sorted不会
sorted(iterable[, cmp[, key[, reverse]]])
sorted(iterable,cmp,key,reverse)
参数:
iterable可以是list或者iterator;
cmp是带两个参数的比较函数;
key 是带一个参数的函数;
reverse为False或者True;
(1)、用cmp函数排序:
>>> list1 = [('david', 90), ('mary',90), ('sara',80),('lily',95)]
>>> sorted(list1,cmp = lambda x,y: cmp(x[0],y[0]))
输出:[('david', 90), ('lily', 95), ('mary', 90), ('sara', 80)]
>>> sorted(list1,cmp = lambda x,y: cmp(x[1],y[1]))
输出:[('sara', 80), ('david', 90), ('mary', 90), ('lily', 95)]
(2)、用key(关键字)函数排序:
>>> list1 = [('david', 90), ('mary',90), ('sara',80),('lily',95)]
>>> sorted(list1,key = lambda list1: list1[0])
输出:[('david', 90), ('lily', 95), ('mary', 90), ('sara', 80)]
>>> sorted(list1,key = lambda list1: list1[1])
输出:[('sara', 80), ('david', 90), ('mary', 90), ('lily', 95)]
(3)、用reverse排序:
>>> sorted(list1,reverse = True)
输出:[('sara', 80), ('mary', 90), ('lily', 95), ('david', 90)]
(4)、用operator.itemgetter函数排序:
>>> from operator import itemgetter
>>> sorted(list1, key=itemgetter(1))
输出:[('sara', 80), ('david', 90), ('mary', 90), ('lily', 95)]
>>> sorted(list1, key=itemgetter(0))
输出:[('david', 90), ('lily', 95), ('mary', 90), ('sara', 80)]
sorted(classCount.iteritems(),key=operator.itemgetter(1),reverse=True) key=operator.itemgetter(1)的意思是按照字典里的第一个排序,{A:1,B:2},要按照第1个(AB是第0个),即‘1’‘2’排序。reverse=True是降序排序
3、argsort(a, axis=-1, kind='quicksort', order=None)函数返回的是数组值从小到大的索引值
(1)、一维数组
>>import numpy as np
>>x = np.array([93,24,11,35])
A、按从小到大排列:
>>np.argsort(x)
输出:array([2, 1, 3, 0])
B、按从大到小排列:
>>np.argsort(-x)
输出:array([0, 3, 1, 2])
(2)、二维数组
>>> x = np.array([[0, 3], [2, 2]])
>>> x
输出:array([[0, 3],
[2, 2]])
A:按列排列
>>> np.argsort(x, axis=0)
输出: array([[0, 1],
[1, 0]])
B、按行排列
>>> np.argsort(x, axis=1)
输出: array([[0, 1],
[0, 1]])
PS: reverse是逆转函数,与此具有相同功能的是:X[::-1]
x = [93,24,11,35]
>>> x[::-1]
[35, 11, 24, 93]
>>> x[:-1]
[93, 24, 11]
部分摘自:http://maoersong.blog.163.com/blog/static/171557351201424105925681/?newFollowBlog
阅读全文
0 0
- Python中sort 、sorted、argsort的区别
- python 中 sort(), sorted(), argsort()
- python--sort,sorted,argsort
- python sort / sorted 和 argsort()
- Python中sort函数、sorted函数和argsort函数
- Python中排序sort 、sorted和argsort函数
- python中排序,函数sort、sorted、argsort,collections.OrderedDict类
- python中argsort,sort 和 sorted,operator.itemgetter函数
- python中sort函数、sorted函数、argsort函数
- Python中排序常用到的sort 、sorted和argsort函数
- Python中排序常用到的sort 、sorted和argsort函数
- Python中排序常用到的sort 、sorted和argsort函数
- Python中排序常用到的sort 、sorted和argsort函数
- Python中排序常用到的sort 、sorted和argsort函数
- Python中排序常用到的sort 、sorted和argsort函数
- Python中排序常用到的sort 、sorted和argsort函数
- python sort()与argsort()区别
- python排序函数sort()、sorted()、argsort()
- nginx1.4.6调用lua
- 51nod 1035 最长的循环节
- xml小结
- Java SE8 Lambda 基础入门---初识lambda
- MTK功能机实现sim卡热插拔功能
- Python中sort 、sorted、argsort的区别
- java加载
- canvas合成图片
- Cpp调用Log4cpp生成日志
- 杭电1052题解
- Redis持久化机制比对
- ioObjectAndProperties
- 【C#】华南理工大学计算机考研复试题目
- 使用Java8的javascript引擎