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所有脚本的主要工作组件.他们都可以在原处进行修改,可以按需求增加或缩短,而且包含任何种类的对象或者被嵌套. 一.列表 列表的主要属性: *任意对象的有序集合 从 ...
随机推荐
- Exp6 信息搜集与漏洞扫描 20164313 杜桂鑫
1.实践目标 掌握信息搜集的最基础技能与常用工具的使用方法. 2.实践内容 (1)各种搜索技巧的应用 1.使用搜索引擎 在百度搜索栏内输入 site:com filetype:doc 北京电子科技学院 ...
- mysql 动态行转列
表结果:create table user( id int , username ), create_time datetime, type int ) insert into user (`id`, ...
- C#解析json和xml数据
C#解析json和xml数据 // 用到的包using Newtonsoft.Json; // using Newtonsoft.Json.Linq; const string value = &qu ...
- LeetCode 86. Partition List 划分链表 C++
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- H5使用codovar插件实现支付宝支付(支付宝APP支付模式,前端)
H5打包的app实现支付及支付宝支付,本章主要详解支付宝支付,微信支付请查看另一篇“H5使用codovar插件实现微信支付(微信APP支付模式,前端)” ps:本文只试用H5开发的,支付宝 APP支付 ...
- Spring Boot实现文件下载功能
我们只需要创建一个控制器(Controler)文件,即Controller目录下的File_Download.java,其完整目录如下: @Controller public class File_D ...
- Android Jetpack 组建介绍(一)——Lifecycler
转自带你领略Android Jetpack组件的魅力 Android Jetpack 对于任何一个产品来说,我们开发中都会面对哪些问题?如:产品交互.用户体验.代码结构.数据获取.数据存储.网络优化. ...
- Go 语言 map (映射)
1.Go 语言中 map 的定义及初始化: map[Key_Type]Value_Type scence := make(map[string]int) 2.Go 语言的遍历: scene := ma ...
- oracle 查看处理锁表
--查出sid,serial#select b.username,b.sid,b.serial#,logon_time from v$locked_object a,v$session b where ...
- centos7编译安装nginx
一.安装依赖包 yum install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre-devel 二 ...