博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python reversed()函数
阅读量:2544 次
发布时间:2019-05-11

本文共 2498 字,大约阅读时间需要 8 分钟。

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 , , etc.

    输入参数必须是一个序列,例如 , , 等。
  • 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, , tuple, list etc.

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

def print_iterator(it):    for x in it:        print(x, end=' ')    print('\n')# reversed stringr = reversed('abc')print(type(r))print(r)print_iterator(r)# reversed listr = reversed([1, 2, 3])print_iterator(r)# reversed tupler = reversed((1, 2, 3))print_iterator(r)# reversed bytesr = reversed(bytes('abc', 'utf-8'))print_iterator(r)# reversed bytearrayr = reversed(bytearray('abc', 'utf-8'))print_iterator(r)

Output:

输出:

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__ methodclass Data:    name = ''    def __init__(self, n):        self.name = n    def __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__ methodclass MyTupleWrapper:    t = ()    def __init__(self, tu):        if not isinstance(tu, tuple):            return ValueError('Only accepts tuple')        self.t = tu    def __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

. 检出完整的python脚本和更多Python示例。

Reference:

参考:

翻译自:

转载地址:http://oqmzd.baihongyu.com/

你可能感兴趣的文章
MySQL存储过程定时任务
查看>>
Python中and(逻辑与)计算法则
查看>>
POJ 3267 The Cow Lexicon(动态规划)
查看>>
设计原理+设计模式
查看>>
音视频处理
查看>>
tomcat 7服务器跨域问题解决
查看>>
前台实现ajax 需注意的地方
查看>>
Jenkins安装配置
查看>>
个人工作总结05(第二阶段)
查看>>
Java clone() 浅拷贝 深拷贝
查看>>
深入理解Java虚拟机&运行时数据区
查看>>
02-环境搭建
查看>>
spring第二冲刺阶段第七天
查看>>
搜索框键盘抬起事件2
查看>>
阿里百川SDK初始化失败 错误码是203
查看>>
透析Java本质-谁创建了对象,this是什么
查看>>
BFS和DFS的java实现
查看>>
关于jquery中prev()和next()的用法
查看>>
一、 kettle开发、上线常见问题以及防错规范步骤
查看>>
eclipse没有server选项
查看>>