Python内置函数reversed()用法分析


Posted in Python onMarch 20, 2018

本文实例讲述了Python内置函数reversed()用法。分享给大家供大家参考,具体如下:

reversed()函数是返回序列seq的反向访问的迭代器。参数可以是列表,元组,字符串,不改变原对象。

1》参数是列表

>>> l=[1,2,3,4,5]
>>> ll=reversed(l)
>>> l
[1, 2, 3, 4, 5]
>>> ll
<listreverseiterator object at 0x06A9E930>
>>> for i in ll:#第一次遍历
...  print i,
... 
5 4 3 2 1
>>> for i in ll:第二次遍历为空,原因见本文最后
...  print i
...

2》参数是列表

>>> l=[3,4,5,6]
>>> ll=reversed(l)
>>> l
[3, 4, 5, 6]
>>> ll
<listreverseiterator object at 0x06A07E10>
>>> list(ll)#第一次
[6, 5, 4, 3]
>>> list(ll)#第二次为空,原因见本文最后
[]

3》参数是元组

>>> t=(4,5,6)
>>> tt=reversed(t)
>>> t
(4, 5, 6)
>>> tt
<reversed object at 0x06A07E50>
>>> tuple(tt)#第一次
(6, 5, 4)
>>> tuple(tt)#第二次为空,原因见本文最后
()

4》参数是字符串

>>> s='cba'
>>> ss=reversed(s)
>>> s
'cba'
>>> ss
<reversed object at 0x06A07E70>
>>> list(ss)#第一次
['a', 'b', 'c']
>>> list(ss)#第二次为空,原因见本文最后
[]

5》参数是字符串

>>> s='1234'
>>> ss=reversed(s)
>>> s
'1234'
>>> ss
<reversed object at 0x06A94490>
>>> ''.join(ss)#第一次
'4321'
>>> ''.join(ss)#第二次为空,原因见本文最后
''

为什么reversed()之后,第二次for循环或第二次list()或第二次tuple()或第二次join()得到的结果为空?我们以第2个例子具体说明一下:

That's because reversed creates an iterator, which is already spent when you're calling list(ll) for the second time.

The reason is that ll is not the reversed list itself, but a listreverseiterator. So when you call list(ll) the first time, it iterates over ll and creates a new list from the items output from that iterator.When you do it a second time, ll is still the original iterator and has already gone through all the items, so it doesn't iterate over anything, resulting in an empty list.

小编来翻译一下:

这是因为反向创建了一个迭代器,该迭代器在第二次调用列表(LL)时已经使用过了。

其原因就是ll不是反转列表本身,而是一个列表反向迭代器。所以当你第一次调用列表(ll),它会遍历ll并且创建一个新的列表从项目输出迭代器。当你再进行一次,ll仍然是原来的迭代器,已经经历了所有的项目,所以它不会再遍历什么,这就造成了空列表。

总结:reversed()之后,只在第一次遍历时返回值。

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
python执行外部程序的常用方法小结
Mar 21 Python
Python新手实现2048小游戏
Mar 31 Python
Python验证码识别的方法
Jul 10 Python
Python进行数据提取的方法总结
Aug 22 Python
Python编程判断一个正整数是否为素数的方法
Apr 14 Python
Python3.5编程实现修改IIS WEB.CONFIG的方法示例
Aug 18 Python
TensorFlow 实战之实现卷积神经网络的实例讲解
Feb 26 Python
Ubuntu下使用Python实现游戏制作中的切分图片功能
Mar 30 Python
python 自动批量打开网页的示例
Feb 21 Python
pandas 选取行和列数据的方法详解
Aug 08 Python
Python命令行参数解析工具 docopt 安装和应用过程详解
Sep 26 Python
使用pytorch和torchtext进行文本分类的实例
Jan 08 Python
shell命令行,一键创建 python 模板文件脚本方法
Mar 20 #Python
python如何拆分含有多种分隔符的字符串
Mar 20 #Python
Python中str.join()简单用法示例
Mar 20 #Python
单利模式及python实现方式详解
Mar 20 #Python
python如何去除字符串中不想要的字符
Jul 05 #Python
python删除某个字符
Mar 19 #Python
Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
Mar 19 #Python
You might like
基于PHP+MySQL的聊天室设计
2006/10/09 PHP
开发大型PHP项目的方法
2006/10/09 PHP
php仿ZOL分页类代码
2008/10/02 PHP
mysql_fetch_row,mysql_fetch_array,mysql_fetch_assoc的区别
2009/04/24 PHP
php str_pad() 将字符串填充成指定长度的字符串
2010/02/23 PHP
php中autoload的用法总结
2013/11/08 PHP
php中的静态变量的基本用法
2014/03/20 PHP
PHP身份证校验码计算方法
2016/08/10 PHP
JavaScript 动态创建VML的方法
2009/10/14 Javascript
javascript demo 基本技巧
2009/12/18 Javascript
一个基于jquery的图片切换效果
2010/07/06 Javascript
基于jquery的button默认enter事件(回车事件)。
2011/05/18 Javascript
js 在定义的时候立即执行的函数表达式(function)写法
2013/01/16 Javascript
jQuery中removeAttr()方法用法实例
2015/01/05 Javascript
javascript实现的淘宝旅行通用日历组件用法实例
2015/08/03 Javascript
轻松理解JavaScript之AJAX
2017/03/15 Javascript
JS/HTML5游戏常用算法之碰撞检测 包围盒检测算法详解【矩形情况】
2018/12/13 Javascript
Vue自定义指令上报Google Analytics事件统计的方法
2019/02/25 Javascript
小程序云开发教程如何使用云函数实现点赞功能
2019/05/18 Javascript
js实现自定义右键菜单
2020/05/18 Javascript
[01:47]2018年度DOTA2最佳教练-完美盛典
2018/12/16 DOTA
[04:00]黄浦江畔,再会英雄——完美世界DOTA2 TI9应援视频
2019/07/31 DOTA
Python3安装Scrapy的方法步骤
2017/11/23 Python
对python中数组的del,remove,pop区别详解
2018/11/07 Python
Python学习笔记之列表推导式实例分析
2019/08/13 Python
css3的transform造成z-index无效解决方案
2014/12/04 HTML / CSS
html5中localStorage本地存储的简单使用
2017/06/16 HTML / CSS
澳大利亚药房在线:ThePharmacy
2017/10/04 全球购物
命名空间(namespace)和程序集(Assembly)有什么区别
2015/09/25 面试题
缴纳养老保险的证明
2014/01/10 职场文书
人事专员工作职责
2014/02/22 职场文书
会计演讲稿范文
2014/05/23 职场文书
防灾减灾日活动总结
2014/08/26 职场文书
交通运输局四风问题对照检查材料思想汇报
2014/10/09 职场文书
大学生个人学习总结
2015/02/15 职场文书
Redis分布式锁Redlock的实现
2021/08/07 Redis