一、列表排序 
# 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数组排序的更多相关文章

  1. julia与python中的列表解析.jl

    julia与python中的列表解析.jl #=julia与python中的列表解析.jl 2016年3月16日 07:30:47 codegay julia是一门很年轻的科学计算语言 julia文档 ...

  2. Python中的列表解析和生成器表达式

    Python中的列表解析和生成器表达式 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.列表解析案例 #!/usr/bin/env python #_*_coding:utf-8 ...

  3. Python中的列表生成式和多层表达式

    Python中的列表生成式和多层表达式 如何生成[1x1, 2x2, 3x3, ..., 10x10]的列表? L=[]; ,): L.append(x*x) print L print (" ...

  4. Python中的列表,元组,字符串之间的相互转化

    Python中的列表元组和字符串之间的相互转化需要利用,tuple(),list(),str(). 示例如下: >>> the_string = "hello I'am x ...

  5. Python中对列表排序实例

    Python中对列表排序实例 发布时间:2015-01-04 09:01:50 投稿:junjie 这篇文章主要介绍了Python中对列表排序实例,本文给出了9个List的排序实例,需要的朋友可以参考 ...

  6. 逗号分隔的字符串转换为Python中的列表 split

    将逗号分隔的字符串转换为Python中的列表   给定一个字符串: 它是由逗号分隔的几个值的序列: mStr = '192.168.1.1,192.168.1.2,192.168.1.3' 如何将字符 ...

  7. 12.python中的列表

    先看列表是如何创建的: a = ['scolia', 123] b = list('scolia',123) 同样有两种创建方式,但一般用第一种. 列表和元祖最大的不同就是列表是可以修改的. 老规矩, ...

  8. python 中的列表解析和生成表达式 - 转

    优雅.清晰和务实都是python的核心价值观,如果想通过操作和处理一个序列(或其他的可迭代对象)来创建一个新的列表时可以使用列表解析(  List comprehensions)和生成表达式,通过这两 ...

  9. Python学习笔记整理(五)Python中的列表.

    列表和字段,这两种类型几乎是Python所有脚本的主要工作组件.他们都可以在原处进行修改,可以按需求增加或缩短,而且包含任何种类的对象或者被嵌套. 一.列表 列表的主要属性: *任意对象的有序集合 从 ...

随机推荐

  1. Android 开发 ConstraintLayout详解

    implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha3' app:layout_constraintHorizo ...

  2. Mac 启用NTFS

    How to Enable NTFS Write Support in Mac OS X http://osxdaily.com/2013/10/02/enable-ntfs-write-suppor ...

  3. Splunk 丰富数据方法

    方法1: 查找 Step 1.创建CSV文件,首字段为索引字段(关联字段) 2.导入CSV文件,Settings, Lookups, Lookup tables files 3.配置Lookup de ...

  4. Timer与TimerTask

    Timer和TimerTask Timer是一种定时器工具,用来在一个后台线程计划执行指定任务.它可以计划执行一个任务一次或反复多次.TimerTask一个抽象类,它的子类代表一个可以被Timer计划 ...

  5. sqoop 问题以及 小tips

    1. Sqoop import 任务里把原来NULL的转化成字符串‘null’了. 解决方法: 先: alter table ${table_name} SET SERDEPROPERTIES('se ...

  6. rest_famework 认证与权限组件

    定义个一个认证类 from rest_framework import exceptionsfrom rest_framework.authentication import BaseAuthenti ...

  7. 44_redux_comment应用_redux版本_同步功能

    项目结构: components里面的东西没变,将app.jsx移动至containers中 /* * 包含所有action的type名称常量 * */ //添加评论 export const ADD ...

  8. Django ORM中常用字段和参数

    一些说明: 表myapp_person的名称是自动生成的,如果你要自定义表名,需要在model的Meta类中指定 db_table 参数,强烈建议使用小写表名,特别是使用MySQL作为后端数据库时. ...

  9. 4.Linux开机设置项

    开机建议优化项: //关闭防火墙 systemctl stop firewalld systemctl disable firewalld //关闭SELinux: setenforce 0 sed ...

  10. Druid中配置双数据库

    配置如下 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://w ...