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所有脚本的主要工作组件.他们都可以在原处进行修改,可以按需求增加或缩短,而且包含任何种类的对象或者被嵌套. 一.列表 列表的主要属性: *任意对象的有序集合 从 ...
随机推荐
- 【转】【测试用例设计】WEB通用测试用例
易用性 1.便于使用.理解.并能减少用户发生错误选择的可能性 2.当数据字段过多时,使用便于用户迅速吸取信息的方式表现信息,突出重点信息,标红等方式 3.显示与当前操作相关的信息,给出操作提示. 4. ...
- bootstrap的使用集锦
在使用div样式的时候可以根据页面布局来调整大小 <div class="col-md-8 col-md-offset-3"># col-md-8 div所占的空间大小 ...
- bootstrap-datetimepicker.js的漢化注意點
1.要引入bootstrap.css ,datetime.picker.css 2.引入的JS文件如下: <script type="text/javascript" src ...
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- Flask框架里的cookie和session
# -*- encoding: utf-8 -*- #cookie 相关的操作,依赖与make_response库,调用cookie依赖request模块 from flask import Flas ...
- orcal - 添加用户、授权
create user jy2 identified by jy2; grant dba to jy2;
- ubuntu16下安装openssh
由于SecureCRT.xshell远程连接ubuntu是通过ssh协议的,所以,需要给ubuntu安装ssh服务器. 1. ssh协议科普 Secure Shell(缩写为SSH),由IETF的网络 ...
- kinect 深度图与彩色图对齐程序
//#include "duiqi.hpp" #include "kinect.h" #include <iostream> #include &q ...
- c#实现文件写入数据表/以二进制流保存到数据库,并实现下载
上传: 1.上传文件先保存到服务器 File.SaveAs(path) 2.sql(文件和sql在一个服务器上)进行保存操作: insert into File(filename,filebody ...
- Sping4之注入参数
Spring的依赖注入不仅可以注入基本类型,也可以注入包括model,list等等类型 package com.hongcong.test; import org.springframework.co ...