Python reversed() function returns a reversed iterator from the specified sequence argument.

Python reversed()函数从指定的sequence参数返回反向迭代器。

python reversed() (Python reversed())

Python reversed() function syntax is:

Python reversed()函数语法为:

reversed(seq)
  • The input argument must be a sequence, for example tuple, list, string etc.输入参数必须是一个序列,例如tuple , list , string等。
  • The returned object is of type reversed and it’s an iterator.返回的对象是reversed类型,它是一个迭代器。
  • We can supply a custom object as reversed() function argument if it has __reversed__() method or supports the sequence protocol.如果自定义对象具有__reversed__()方法或支持序列协议,则可以提供它作为reversed()函数参数。
  • We need to implement __len__() method and the __getitem__() method with integer arguments starting at 0 to support sequence protocol.我们需要使用从0开始的整数参数来实现__len__()方法和__getitem__()方法,以支持序列协议。

带序列的reversed() (reversed() with sequence)

Let’s look at reversed() function examples with standard sequence objects such as string, bytes, tuple, list etc.

让我们看一下带有标准序列对象(例如字符串, 字节 ,元组,列表等)的reversed()函数示例。

def print_iterator(it):for x in it:print(x, end=' ')print('\n')# reversed string
r = reversed('abc')
print(type(r))
print(r)
print_iterator(r)# reversed list
r = reversed([1, 2, 3])
print_iterator(r)# reversed tuple
r = reversed((1, 2, 3))
print_iterator(r)# reversed bytes
r = reversed(bytes('abc', 'utf-8'))
print_iterator(r)# reversed bytearray
r = reversed(bytearray('abc', 'utf-8'))
print_iterator(r)

Output:

输出:

<class 'reversed'>
<reversed object at 0x109d1f208>
c b a 3 2 1 3 2 1 99 98 97 99 98 97

Notice that bytes and bytearray elements are converted to integer while printing on console.

请注意,在控制台上打印时,字节和字节数组元素将转换为整数。

具有具有__reversed__方法的对象的reversed() (reversed() with object having __reversed__ method)

# object with __reversed__ method
class Data:name = ''def __init__(self, n):self.name = ndef __reversed__(self):return reversed(self.name)d = Data('ABC')r = reversed(d)print_iterator(r)

Output: C B A

输出: CBA

具有对象支持序列协议的reversed() (reversed() with object supporting sequence protocol)

# object supporting sequence protocol i.e.
# implementing __len__() and __getitem__ method
class MyTupleWrapper:t = ()def __init__(self, tu):if not isinstance(tu, tuple):return ValueError('Only accepts tuple')self.t = tudef __len__(self):return len(self.t)def __getitem__(self, index):return self.t[index]mt = MyTupleWrapper((1, 2, 3, 4))
r = reversed(mt)
print_iterator(r)

Output: 4 3 2 1

输出: 4 3 2 1

GitHub Repository.GitHub存储库中检出完整的python脚本和更多Python示例。

Reference: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/23113/python-reversed-function

Python reversed()函数相关推荐

  1. [转载] Python reversed函数及用法【小白学习Python必备知识】

    参考链接: Python reversed() reserved() 是 Pyton 内置函数之一,其功能是对于给定的序列(包括列表.元组.字符串以及 range(n) 区间),该函数可以返回一个逆序 ...

  2. python reversed函数判断回文

    a = 'abcba'b = reversed(a) # next()后 ["a","b","c","b"," ...

  3. Python reversed函数及用法

    reserved() 是 Pyton 内置函数之一,其功能是对于给定的序列(包括列表.元组.字符串以及 range(n) 区间),该函数可以返回一个逆序序列的迭代器(用于遍历该逆序序列). reser ...

  4. python的reversed函数实质_Python reversed函数及使用方法解析

    Python reversed函数及使用方法解析 发布时间:2020-09-09 01:07:18 来源:脚本之家 阅读:74 有些时候,程序需要进行反向遍历,此时可通过 reversed() 函数, ...

  5. python中reversed是什么意思_Python reversed函数及使用方法解析

    Python reversed函数及使用方法解析 有些时候,程序需要进行反向遍历,此时可通过 reversed() 函数,该函数可接收各种序列(元组.列表.区间等)参数,然后返回一个"反序排 ...

  6. python中reversed用法_Python中reversed函数有哪些功能呢?

    摘要: 下文讲述Python中reversed函数的功能简介说明,如下所示: reversed函数是Python的内置函数 reversed函数功能: 将一个序列进行反向排列 reversed函数语法 ...

  7. python字符串函数reversed_Python reversed()函数

    # Python `reversed()`函数 > 原文: [https://thepythonguru.com/python-builtin-functions/reversed/](http ...

  8. Python中reversed()函数

    python中reversed()函数 reversed()函数返回的是反向迭代器对象,而不是列表 num=[1,2,3,4,5] #一个列表 print(reversed(num))#结果为*< ...

  9. 【python基础】reverse和reversed函数及使用

    一.reverse reverse()是python中列表的一个内置方法(在字典.字符串和元组中没有这个内置方法),用于列表中数据的反转 例子: lista = [1, 2, 3, 4] lista. ...

最新文章

  1. 在Android中,如何以编程方式在dp中设置边距?
  2. windows下python安装Numpy和Scipy模块
  3. eclipse中启动tomcat,不能访问localhost解决办法
  4. ServiceStack.Redis——Redis于.net向上client解
  5. 使用Python构建推荐系统的机器学习
  6. 如何在ASP.NET Core 中快速构建PDF文档
  7. 同一个工作组无法查看计算机,win7系统在同一个工作组看不到其他电脑的解决方法...
  8. 安卓流行布局开源库_如何使用流行度在开源库之间进行选择
  9. java nextlong_Java Random nextLong()方法与示例
  10. Ubuntu enca命令可以用来查看文件的编码格式
  11. SpringMVC 工作流程
  12. minst 手写数字识别实战
  13. 金蝶EAS,序时簿界面数据背景色,根据枚举值设置背景色
  14. keil的错误: Error: Encountered an improper argument 的解决方法
  15. php常用单词上传知米背单词,知米背单词APP导入单词列表的操作步骤
  16. JMeter jp@gc - stepping thread group插件
  17. 【leetcode慢速刷题记录】1. 两数之和
  18. centos7应用笔记:键盘重影射
  19. 基于Q的Android开机动画
  20. JSD-2204-酷鲨商城(管理商品模块)-Day02

热门文章

  1. Win7 XAMPP apache无法启动的问题
  2. 修改web.conf不重启服务的方法
  3. [转载] python中pass的使用_Python pass详细介绍及实例代码
  4. [转载] python中的且语句_简单探讨python中的语句和语法
  5. [转载] 五、字符串类的实现及子串查找算法
  6. [转载] python截取指定字符串_python字符串截取,python字符串切片的方法详解
  7. [转载] python的 for、while循环、嵌套循环
  8. MySQL之存储引擎,数据类型,约束条件
  9. Oracle VM VirtualBox 随系统自动启动虚拟机的方法
  10. C++ Primer Plus学习(七)——函数简介