Python列表排序 reverse、sort、sorted 操作方法详解
python语言中的列表排序方法有三个:reverse反转/倒序排序、sort正序排序、sorted可以获取排序后的列表。在更高级python list排序中,后两中方法还可以加入条件参数进行排序。
reverse()方法
将列表中元素反转排序,比如下面这样
>>> x = [1,5,2,3,4]
>>> x.reverse()
>>> x
[4, 3, 2, 5, 1]
reverse列表反转排序:是把原列表中的元素顺序从左至右的重新存放,而不会对列表中的参数进行排序整理。如果需要对列表中的参数进行整理,就需要用到列表的另一种排序方式sort正序排序。
sort()排序方法
此函数方法对列表内容进行正向排序,排序后的新列表会覆盖原列表(id不变),也就是sort排序方法是直接修改原列表list排序方法。
>>> a = [5,7,6,3,4,1,2]
>>> a.sort()
>>> a
[1, 2, 3, 4, 5, 6, 7]
在玩蛇网许多python初学者,对sort()方法比较糊涂。有的时候会需要一个排序好的列表,而又想保存原有未排序列表,他们会这么操作:
>>> a = [5,7,6,3,4,1,2]
>>> b = a.sort()
>>> print b
None
这个时候问题出现了,变量b得到的是一个空值。那么想要得到排序好的列表,又想保留原列表怎么办呢?列表sorted()方法可以帮你实现。
sorted()方法
即可以保留原列表,又能得到已经排序好的列表sorted()操作方法如下:
>>> a = [5,7,6,3,4,1,2]
>>> b = sorted(a)
>>> a
[5, 7, 6, 3, 4, 1, 2]
>>> b
[1, 2, 3, 4, 5, 6, 7]
sorted()方法可以用在任何数据类型的序列中,返回的总是一个列表形式:
>>> sorted('iplaypython.com')
['.', 'a', 'c', 'h', 'i', 'l', 'm', 'n', 'o', 'o', 'p', 'p', 't', 'y', 'y']
本文讲解的是Python列表最基础的排序方法,列表还有更高级的排序方法,比如可以在方法内加入排序条件,这些会在列表高级排序方法文章中为大家介绍。
浏览这篇文章的网友,正在看:
Python 100例 练习题
树莓派python编程
正则表达式
JSON教程
Apache配置
MySQL数据库
Python标签页
更多推荐页面:
python招聘2页,3页,4页,5页,6页,7页,8页,9页,10页,11页,12页,13页
seo与python_2页,3页,4页
python资讯2页,3页
python源码2页,3页,4页,5页,6页
玩蛇网文章,转载请注明出处和文章网址:https://www.iplaypy.com/jinjie/jj114.html
相关文章 Recommend
- • 2019年3月最新消息: Python 3.4.10 现已推出
- • [上海]招Python量化系统开发工程师
- • 优集品网络科技有限公司招Python中/高级工程师
- • 爱因互动科技发展有限公司招募Python开发攻城狮
- • mozio招聘Python/Django工程师
- • Kavout金融科技公司招Python研发工程师
- • Python数组逆向输出,编程练习题实例四十
- • Python数组插入排序,编程练习题实例三十九
- • Python矩阵for循环应用,编程练习题实例三十八
- • Python操作Redis数据库方面的问题
- • 请python高手帮我看看这段python代码中函数setter的
- • Python什么方法可以快速将两个队列变成字典
PYTHON基础入门必备教程 Must Know PYTHON Tutorials
- • python 解释器
- • python idle
- • python dir函数
- • python 数据类型
- • python type函数
- • python 字符串
- • python 整型数字
- • python 列表
- • python 元组
- • python 字典
- • python 集合
- • python 变量
- • python print
- • python 函数
- • python 类定义
- • python import
- • python help
- • python open
- • python 异常处理
- • python 注释
- • python continue
- • python pass
- • python return
- • python global
- • python assert
- • python if语句
- • python break
- • python for循环
- • python while循环
- • python else/elif
- • lambda匿名函数
PYTHON进阶提高必备教程 Must Know PYTHON Modules
- • os 模块
- • sys 模块
- • re 正则表达式
- • time 日期时间
- • pickle 持久化
- • random 随机
- • csv 模块
- • logging 日志
- • socket网络通信
- • json模块
- • urlparse 解析URL
- • urllib 模块
- • urllib2 模块
- • robotparser 解析
- • Cookie 模块
- • smtplib 邮件
- • Base64 编码
- • xmlrpclib客户端
- • string 文本
- • Queue 线程安全
- • math数学计算
- • linecache缓存
- • threading多线程
- • sqlite3数据库
- • gzip压缩解压
最新内容 NEWS
- • Python列表添加 insert() 插入元素方法
- • Python列表删除 pop() 方法返回指定对象
- • Python else与elif语句语法讲解
- • Python IOError错误异常原因
- • Python列表排序 reverse、sort、sorted 操作方法
- • 使用 from import方法导入Python模块
- • Python break语句 跳出循环
- • Python return语句 函数返回值
- • Python import语句导入模块语法
- • python assert 断言详细用法格式
图文精华 RECOMMEND
-
Python列表添加 insert() 插入元素方
-
Python列表删除 pop() 方法返回指定
-
Python else与elif语句语法讲解
-
Python列表排序 reverse、sort、sorte
-
Python break语句 跳出循环
-
Python return语句 函数返回值
热点文章 HOT
- Python语言的一些基本常用语句
- Python 字典items返回列表,iteritems返回迭代
- Python函数的形参和实参详解
- Python is同一性运算符和==相等运算符区别
- Python for循环控制语句一般格式及方法
- Python 序列的概念及基本操作方法
- python assert 断言详细用法格式
- python if控制流语句 语法笔记
- Python 函数的关键字参数和位置参数
- Python条件语句的嵌套使用