Python内置函数(54)——reversed
英文文档:
reversed(seq)- Return a reverse iterator. seq must be an object which has a
__reversed__()method or supports the sequence protocol (the__len__()method and the__getitem__()method with integer arguments starting at0).
说明:
1. 函数功能是反转一个序列对象,将其元素从后向前颠倒构建成一个新的迭代器。
>>> a = reversed(range(10)) # 传入range对象
>>> a # 类型变成迭代器
<range_iterator object at 0x035634E8>
>>> list(a)
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0] >>> a = ['a','b','c','d']
>>> a
['a', 'b', 'c', 'd']
>>> reversed(a) # 传入列表对象
<list_reverseiterator object at 0x031874D0>
>>> b = reversed(a)
>>> b # 类型变成迭代器
<list_reverseiterator object at 0x037C4EB0>
>>> list(b)
['d', 'c', 'b', 'a']
2. 如果参数不是一个序列对象,则其必须定义一个__reversed__方法。
# 类型Student没有定义__reversed__方法
>>> class Student:
def __init__(self,name,*args):
self.name = name
self.scores = []
for value in args:
self.scores.append(value) >>> a = Student('Bob',78,85,93,96)
>>> reversed(a) # 实例不能反转
Traceback (most recent call last):
File "<pyshell#37>", line 1, in <module>
reversed(a)
TypeError: argument to reversed() must be a sequence
>>> type(a.scores) # 列表类型
<class 'list'> # 重新定义类型,并为其定义__reversed__方法
>>> class Student:
def __init__(self,name,*args):
self.name = name
self.scores = []
for value in args:
self.scores.append(value)
def __reversed__(self):
self.scores = reversed(self.scores) >>> a = Student('Bob',78,85,93,96)
>>> a.scores # 列表类型
[78, 85, 93, 96]
>>> type(a.scores)
<class 'list'> >>> reversed(a) # 实例变得可以反转
>>> a.scores # 反转后类型变成迭代器
<list_reverseiterator object at 0x0342F3B0>
>>> type(a.scores)
<class 'list_reverseiterator'> >>> list(a.scores)
[96, 93, 85, 78]
Python内置函数(54)——reversed的更多相关文章
- Python内置函数(36)——reversed
英文文档: reversed(seq) Return a reverse iterator. seq must be an object which has a __reversed__() meth ...
- Python内置函数(54)——callable
英文文档: callable(object) Return True if the object argument appears callable, False if not. If this re ...
- Python内置函数reversed()用法分析
Python内置函数reversed()用法分析 这篇文章主要介绍了Python内置函数reversed()用法,结合实例形式分析了reversed()函数的功能及针对序列元素相关操作技巧与使用注意事 ...
- Python | 内置函数(BIF)
Python内置函数 | V3.9.1 | 共计155个 还没学完, 还没记录完, 不知道自己能不能坚持记录下去 1.ArithmeticError 2.AssertionError 3.Attrib ...
- python内置函数
python内置函数 官方文档:点击 在这里我只列举一些常见的内置函数用法 1.abs()[求数字的绝对值] >>> abs(-13) 13 2.all() 判断所有集合元素都为真的 ...
- python 内置函数和函数装饰器
python内置函数 1.数学相关 abs(x) 取x绝对值 divmode(x,y) 取x除以y的商和余数,常用做分页,返回商和余数组成一个元组 pow(x,y[,z]) 取x的y次方 ,等同于x ...
- Python 内置函数笔记
其中有几个方法没怎么用过, 所以没整理到 Python内置函数 abs(a) 返回a的绝对值.该参数可以是整数或浮点数.如果参数是一个复数,则返回其大小 all(a) 如果元组.列表里面的所有元素都非 ...
- 【转】python 内置函数总结(大部分)
[转]python 内置函数总结(大部分) python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为 ...
- python内置函数,匿名函数
一.匿名函数 匿名函数:为了解决那些功能很简单的需求而设计的一句话函数 def calc(n): return n**n print(calc(10)) #换成匿名函数 calc = lambda n ...
随机推荐
- 20175305张天钰Java结对编程四则运算
Java结对编程四则运算 一.题目描述:如何对表达式进行求值运算呢 1.中缀表达式与后缀表达式(娄老师讲解) 中缀表达式就是运算符号在运算数中间的表达式,比如1+2,顾名思义,后缀表达式就是运算符在运 ...
- idea看源码
idea看源码,可以直接搜索.看接口具体调用的是哪个类里面的方法(多态)
- 二、自动化测试平台搭建-搭建jango环境
上篇说的是安装虚拟环境,后面的项目全部放在虚拟环境上 1.创建一个虚拟环境py3,进入虚拟环境 2.安装django包:pip install django==1.8.2 3.在家目录下的Deskto ...
- iOS报错:linker command failed with exit code 1 (use -v to see invocation) 问题解决方式之一
百度库原版本:3.2.1 更新为:4.2.0,两个库相隔2年时间: 问题i: 更新CocoaPods的同时更新了百度地图库的版本,运行程序报错: linker command failed with ...
- STM8L052低功耗模式
Stm8L系列单片机的低功耗有五种模式: § wait模式 § Lowpower run模式 § Lowpower wait模式 § Active-haltwith full RTC模式 § Halt ...
- Influxdb+Grafana+Telegraf及docker中运行
目录 参考资料 1. InfluxDB 1. 特征: 2. 特点: 3. 功能及默认 4. 主要概念 1) 与SQL的名词做比较 2) InfluxDB的独有概念 5. 常用命令 1. 用户管理: 6 ...
- C++第一课:基本语法for Visual Studio 2015[个人见解]
在学习C++时,或许不了解情况的人会问:到底先学习C语言还是C++,哪个更好? 那么小编的个人见解是:你在学习时别管哪个语言好与不好,是个语言它都是好语言,关键在于你会挖掘其中存在的价值,C++可以说 ...
- iOS 上传自己的工程(模块工具类)到cocoapods上遇到坑
最近在研究把自己写的工具类和模块上传到cocoapods上, 再新构建项目中可以直接使用cocoapods使用 也可以更新之前的版本 便于维护项目. 但是在这个过程中遇到了种种问题 但是最后还是解决 ...
- 等高布局display:table
display:table用法: 父元素:display:table; 子元素:display:table-cell; 注:使用display:table-cell元素部分出现空白缝隙的问题:设置 . ...
- python绘制图
如何用python绘制图表 摘要: 使用python绘制简单的图表,包括折线图.柱状图.条形图.饼图.散点图.气泡图.箱线图.直方图等. 前言 本文介绍如果使用python汇总常用的图表,与Excel ...