英文文档:

reversed(seq)
Return a reverse iteratorseq 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 at 0).  

  反转序列生成新的可迭代对象

说明:

  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内置函数(36)——reversed的更多相关文章

  1. Python内置函数(36)——iter

    英文文档: iter(object[, sentinel]) Return an iterator object. The first argument is interpreted very dif ...

  2. Python内置函数(54)——reversed

    英文文档: reversed(seq) Return a reverse iterator. seq must be an object which has a __reversed__() meth ...

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

    Python内置函数reversed()用法分析 这篇文章主要介绍了Python内置函数reversed()用法,结合实例形式分析了reversed()函数的功能及针对序列元素相关操作技巧与使用注意事 ...

  4. 【转】python 内置函数总结(大部分)

    [转]python 内置函数总结(大部分) python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为 ...

  5. python 内置函数总结(大部分)

    python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为我们提供了68个内置函数.它们就是pytho ...

  6. Python内置函数和内置常量

    Python内置函数 1.abs(x) 返回一个数的绝对值.实参可以是整数或浮点数.如果实参是一个复数,返回它的模. 2.all(iterable) 如果 iterable 的所有元素为真(或迭代器为 ...

  7. Python | 内置函数(BIF)

    Python内置函数 | V3.9.1 | 共计155个 还没学完, 还没记录完, 不知道自己能不能坚持记录下去 1.ArithmeticError 2.AssertionError 3.Attrib ...

  8. python内置函数

    python内置函数 官方文档:点击 在这里我只列举一些常见的内置函数用法 1.abs()[求数字的绝对值] >>> abs(-13) 13 2.all() 判断所有集合元素都为真的 ...

  9. python 内置函数和函数装饰器

    python内置函数 1.数学相关 abs(x) 取x绝对值 divmode(x,y) 取x除以y的商和余数,常用做分页,返回商和余数组成一个元组 pow(x,y[,z]) 取x的y次方 ,等同于x ...

随机推荐

  1. SpringMVC【开发Controller】详解

    前言 本文主要是讲解在Controller中的开发,主要的知识点有如下: 编码过滤器 使用注解开发 注解@RequestMapping详解 业务方法接收参数 字符串转日期 重定向和转发 返回JSON ...

  2. 数组Array、数组API

    1.数组:批量管理多个数据的存储空间. 数组的作用:现实中,批量管理多个数据都是集中分组存放,良好的数据结构,可极大提高程序的执行效率! 优点:方便查找 2.创建数组:(4种方式) (1)var 变量 ...

  3. JavaScript中的execCommand

    execCommand方法是执行一个对当前文档,当前选择或者给出范围的命令.处理Html数据时常用 如下格式:document.execCommand(sCommand[,交互方式, 动态参数]) , ...

  4. 如何在WordPress文本小工具中使用PHP

    只需添加以下代码片段到你当前主题的functions.php文件 add_filter('widget_text', 'php_text', 99); function php_text($text) ...

  5. Linux find用法

    Linux中find常见用法示例 ----摘抄哪里忘记了 ·find   path   -option   [   -print ]   [ -exec   -ok   command ]   {} ...

  6. java创建运行以及项目结构

    一 创建java project 再src下添加class,选择一个class添加main方法作为程序的入口 二.项目结构: src下添加不同的包,命名方法为com.jikexueyuan.hello ...

  7. poj 1562 dfs

    http://poj.org/problem?id=1562 #include<iostream> using namespace std; ,m=,sum=; ][]; ][]={-,, ...

  8. iOS CocoaPods一些特别的用法 指定版本、版本介绍、忽略警告

    简介 介绍一些CocoaPods一些特别的用法 CocoaPods github地址 CocoaPods 官方地址 1. 指定第三方库版本 1. 固定版本 target 'MyApp' do use_ ...

  9. 网络通信 --> select()用法

    select()用法 头文件 #include <sys/time.h> #include <sys/types.h> #include <unistd.h> 定义 ...

  10. JSON字符串转为JSON对象

    在数据传输流程中,json是以文本,即字符串的形式传递的,而JS操作的是对象,所以,JSON对象(js对象)和JSON字符串之间的相互转换是关键. JSON可以有两种格式,一种是对象格式的,另一种是数 ...