python中的列表及numpy数组排序
一、列表排序
# python中对列表排序有sort、sorted两种方法,其中sort是列表内置方法,其帮助文档如下:
In [1]: 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. In [2]: help(list.sort)
Help on method_descriptor:
sort(...)
L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*
# 一维列表排序
a = [2,1,4,3,5]
a.sort() #[1, 2, 3, 4, 5]
a.sort(reverse=True) #[5, 4, 3, 2, 1]
# sorted()用法同sort(),不同点在于sorted()方法不影响原列表元素的顺序
a = [2,1,4,3,5]
b = sorted(a) # b: [1, 2, 3, 4, 5]
a # a: [2, 1, 4, 3, 5]
# 二维列表排序
# 二维列表排序,可以设置sort()和sorted()方法的key关键字,通过lambda函数指定排序关键字
a = [[1,'b',5],[3,'c',3],[5,'a',4],[4,'d',1],[2,'f',2]]
a.sort(key=(lambda x:x[0])) # [[1, 'b', 5], [2, 'f', 2], [3, 'c', 3], [4, 'd', 1], [5, 'a', 4]]
a.sort(key=(lambda x:x[1])) # [[5, 'a', 4], [1, 'b', 5], [3, 'c', 3], [4, 'd', 1], [2, 'f', 2]]
a.sort(key=(lambda x:x[0]),reverse=True) # reverse=True 按照x[0]降序排列 [[5, 'a', 4], [4, 'd', 1], [3, 'c', 3], [2, 'f', 2], [1, 'b', 5]]
二、numpy数组排序
1. numpy.sort()
# numpy.sort()
In [3]: help(np.sort)
Help on function sort in module numpy.core.fromnumeric: sort(a, axis=-1, kind='quicksort', order=None)
Return a sorted copy of an array. Parameters
----------
a : array_like
Array to be sorted.
axis : int or None, optional
Axis along which to sort. If None, the array is flattened before
sorting. The default is -1, which sorts along the last axis.
kind : {'quicksort', 'mergesort', 'heapsort'}, optional
Sorting algorithm. Default is 'quicksort'.
order : str or list of str, optional
When `a` is an array with fields defined, this argument specifies
which fields to compare first, second, etc. A single field can
be specified as a string, and not all fields need be specified,
but unspecified fields will still be used, in the order in which
they come up in the dtype, to break ties. Returns
-------
sorted_array : ndarray
Array of the same type and shape as `a`.
In [4]: a
Out[1]:
array([['', 'b', ''],
['', 'c', ''],
['', 'a', ''],
['', 'd', ''],
['', 'f', '']], dtype='<U11') In [5]: np.sort(a,axis=0) # axis=0 按列跨行排序
Out[2]:
array([['', 'a', ''],
['', 'b', ''],
['', 'c', ''],
['', 'd', ''],
['', 'f', '']], dtype='<U11') In [6]: np.sort(a,axis=1) # axis=1 按行跨列排序
Out[3]:
array([['', '', 'b'],
['', '', 'c'],
['', '', 'a'],
['', '', 'd'],
['', '', 'f']], dtype='<U11')
2. numpy.msort()
# numpy.msort() 与numpy.sort()基本相同,只是没有axis参数,相当于numpy.sort()在axis=0时的特殊情况
In [7]: np.msort(a)
Out[4]:
array([['', 'a', ''],
['', 'b', ''],
['', 'c', ''],
['', 'd', ''],
['', 'f', '']], dtype='<U1')
numpy中还有ndarray.sort()、argsort()、lexsort()以及复数排序sort_complex()方法,用到后再学习记录,待续……
python中的列表及numpy数组排序的更多相关文章
- julia与python中的列表解析.jl
julia与python中的列表解析.jl #=julia与python中的列表解析.jl 2016年3月16日 07:30:47 codegay julia是一门很年轻的科学计算语言 julia文档 ...
- Python中的列表解析和生成器表达式
Python中的列表解析和生成器表达式 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.列表解析案例 #!/usr/bin/env python #_*_coding:utf-8 ...
- Python中的列表生成式和多层表达式
Python中的列表生成式和多层表达式 如何生成[1x1, 2x2, 3x3, ..., 10x10]的列表? L=[]; ,): L.append(x*x) print L print (" ...
- Python中的列表,元组,字符串之间的相互转化
Python中的列表元组和字符串之间的相互转化需要利用,tuple(),list(),str(). 示例如下: >>> the_string = "hello I'am x ...
- Python中对列表排序实例
Python中对列表排序实例 发布时间:2015-01-04 09:01:50 投稿:junjie 这篇文章主要介绍了Python中对列表排序实例,本文给出了9个List的排序实例,需要的朋友可以参考 ...
- 逗号分隔的字符串转换为Python中的列表 split
将逗号分隔的字符串转换为Python中的列表 给定一个字符串: 它是由逗号分隔的几个值的序列: mStr = '192.168.1.1,192.168.1.2,192.168.1.3' 如何将字符 ...
- 12.python中的列表
先看列表是如何创建的: a = ['scolia', 123] b = list('scolia',123) 同样有两种创建方式,但一般用第一种. 列表和元祖最大的不同就是列表是可以修改的. 老规矩, ...
- python 中的列表解析和生成表达式 - 转
优雅.清晰和务实都是python的核心价值观,如果想通过操作和处理一个序列(或其他的可迭代对象)来创建一个新的列表时可以使用列表解析( List comprehensions)和生成表达式,通过这两 ...
- Python学习笔记整理(五)Python中的列表.
列表和字段,这两种类型几乎是Python所有脚本的主要工作组件.他们都可以在原处进行修改,可以按需求增加或缩短,而且包含任何种类的对象或者被嵌套. 一.列表 列表的主要属性: *任意对象的有序集合 从 ...
随机推荐
- Windows下安装MySQL5.7.18的方法
准备: 操作系统:win7 下64位的zip版本的MySQL,路径:http://dev.mysql.com/downloads/mysql/ 我下的是最新版的MySQL,解压后,目录如下: 可以看到 ...
- Android Jetpack 组建介绍(二)——Lifecycler
参考Android Jetpack架构组件之 Lifecycle(源码篇) 源码分析 关于Lifecycle的使用考上一篇文章Android Jetpack框架之 Lifecycles(使用篇),从使 ...
- Python第7天
其他内置函数: abs() 绝对值 all()均为真则为True any()有一个为真就为True bin()十进制->二进制 bool() 空,0,None为False,其余为True byt ...
- Git 切换本地分支 切换远程分支
切换本地分支 git checkout work1 切换到新的分支工作(不存在则会创建) 将本地已有的分支(已经存在) 和 远程分支连接 git branch --set-upstream-to=or ...
- 服务器还原阿里云Mysql数据库
https://www.percona.com/doc/percona-xtrabackup/2.3/installation/yum_repo.html
- 用python发送短消息(基于阿里云平台)
新版短信接口在线测试页面:https://api.aliyun.com/new#/?product=Dysmsapi&api=SendSms¶ms={}&tab=DEM ...
- js中的变异数组
[ 'push', 'pop', 'shift', 'unshift', 'splice', 'sort', 'reverse' ] 以上几个数组的方法会改变原数组,称之为数组的变异方法.
- 关于第一个launcher开发笔记
本笔记主要记录阅读关于launcher代码是的相关知识点. viewpager的简单使用(适配器模式):https://www.cnblogs.com/fuly550871915/p/4922953. ...
- 云笔记项目-Spring事务学习-传播NOT_SUPPORTED
接下来测试事务传播属性设置为NOT_SUPPORTED Service层 Service层主要设置如下,其中还插入了REQUIRED作为比较. package Service; import java ...
- linux 安装 kafka&zookeeper
安装kafka 1,下载kafka. #cd /usr/local #wget wget https://mirrors.tuna.tsinghua.edu.cn/apache/kafka/2.1.1 ...