欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

Python reversed反转序列并生成可迭代对象

时间:2021-05-14 10:39:53|栏目:Python代码|点击:

英文文档:

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 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 爬虫请求模块requests详解

栏    目:Python代码

下一篇:python线程池的实现实例

本文标题:Python reversed反转序列并生成可迭代对象

本文地址:http://www.codeinn.net/misctech/121377.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有