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所有脚本的主要工作组件.他们都可以在原处进行修改,可以按需求增加或缩短,而且包含任何种类的对象或者被嵌套. 一.列表 列表的主要属性: *任意对象的有序集合 从 ...
随机推荐
- LeetCode 142. Linked List Cycle II 判断环入口的位置 C++/Java
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To r ...
- python远程连接windows
远程连接windows系统 https://blog.51cto.com/ckl893/2145809 import winrm win2008 = winrm.Session('http:/ ...
- mac tomcat安装
https://blog.csdn.net/qq_35106903/article/details/78860121
- ichartjs用法
代码 <script type="text/javascript" src="../js/ichart.1.2.min.js"></scrip ...
- 发布python包
写python程序时常常要导入各种包,使用其中的模块或功能.我们如果有可以复用的功能或模块也可以发布成包,并安装在自己或他人电脑上,以供自己或别人使用.python发布包的步骤如下: 一.首先为模块创 ...
- Word2vec教程
Word2vec Tutorial RADIM ŘEHŮŘEK 2014-02-02GENSIM, PROGRAMMING157 COMMENTS I never got round to writi ...
- Oracle 11gR2 客户端windows 10安装后PL/SQL配置
操作系统:windows 10 软件:Oracle 11gR2 客户端 (64 bit) PLSQL Developer 13 (64 bit) 注意:PLSQL与oracle客户端版本要一致 1. ...
- Windows 7升级1月更新汇总后导致SMBv2网络无法正常工作
在本月的补丁星期二活动日中,微软面向Windows 7.Windows 2008 R2服务器系统推出了KB4480970的月度更新汇总.然而根据部分用户反馈,在安装该更新之后导致系统的网络设置无法正常 ...
- zookeeper学习笔记记录
zookeeper的概述: ZooKeeper 本质上是一个分布式的小文件存储系统.提供基于类似于文件系统的目录树方式的数据存储,并且可以对树中的节点进行有效管理.从而用来维护和监控你存储的数据的状态 ...
- thinkphp5 or
$where['sq']=[ [ 'like' , '%"'.UID.'"%'] , [ 'like' , '%"'.$userinfo['depart_id'].'&q ...