认识reversed单词
reversed 英[rɪ'vɜ:st] 美[rɪ'vɜst]
adj. 颠倒的;相反的;(判决等)撤销的
v. 颠倒(reverse的过去式和过去分词);翻转
help(reversed)
Help on class reversed in module builtins: class reversed(object) | reversed(sequence) -> reverse iterator over values of the sequence | | Return a reverse iterator | | Methods defined here: | | __getattribute__(self, name, /) | Return getattr(self, name). | | __iter__(self, /) | Implement iter(self). | | __length_hint__(...) | Private method returning an estimate of len(list(it)). | | __new__(*args, **kwargs) from builtins.type | Create and return a new object. See help(type) for accurate signature. | | __next__(self, /) | Implement next(self). | | __reduce__(...) | Return state information for pickling. | | __setstate__(...) | Set state information for unpickling.
reversed的英文解释
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).
reversed()函数的输入时任意一个序列,返回一份倒序后的序列副本。通常用于for循环需要倒序循环时。
eg:
seq = [1, 2, 3, 4, 5, 6, 7, 8] for item in reversed(seq): print(item, end=" ")
结果:
8 7 6 5 4 3 2 1
reversed()的功能:翻转对象
- 翻转函数reversed()调用参数类中的__reversed__()函数。
- 函数功能是反转一个序列对象,将其元素从后向前颠倒构建成一个新的迭代器
reversed()的应用
应用1: 序列的倒序循环
seq1 = list(range(8)) print("seq1为: {}".format(seq1)) for i in reversed(seq1): print(i, end=' ')
结果:
seq1为: [0, 1, 2, 3, 4, 5, 6, 7] 7 6 5 4 3 2 1 0
seq2 = ['a', 'b', 'c', 'd'] print("seq2为: {}".format(seq2)) for i in reversed(seq2): print(i, end=' ')
结果:
seq2为: ['a', 'b', 'c', 'd'] d c b a
应用2:
如果一个对象不存在__reversed__()方法,那么,reversed()会调用__len__()和__getitem__()生成倒序序列。
class MySeq(): def __len__(self): return 6 def __getitem__(self, index): return 'y{0}'.format(index) for item in reversed(MySeq()): print(item, end=', ')
结果:
y5, y4, y3, y2, y1, y0,
应用3:
如果我们需要定制或者优化倒序过程,我们只需重写__reversed__()方法。
class MySeq(): def __len__(self): return 6 def __getitem__(self, index): return 'y{0}'.format(index)
class MyReversed(MySeq): def __reversed__(self): return 'hello, bright!' for item in reversed(MyReversed()): # 调用重写的__reversed__()方法。 print(item, end=' ')
结果:
h e l l o , b r i g h t !
或者:
class MySeq(): def __len__(self): return 6 def __getitem__(self, index): return 'y{0}'.format(index) class MyReversed(MySeq): def __reversed__(self): return reversed('hello, bright!') for item in reversed(MyReversed()): print(item, end=' ')
结果:
! t h g i r b , o l l e h
应用4:
如果reversed()的参数不是一个序列对象,我们应该为该对象定义一个__reversed__方法,这个时候就可以使用reversed()函数了。
class MyScore: 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) x = MyScore("bright", 66, 77, 88, 99, 100) print('我的名字: {0}, \n我的成绩: {1}'.format(x.name, x.scores)) print('我的成绩按降序排列:{}'.format(list(reversed(x.scores))))
结果:
我的名字: bright, 我的成绩: [66, 77, 88, 99, 100] 我的成绩按降序排列:[100, 99, 88, 77, 66]
知识在于点点滴滴的积累,我会在这个路上Go ahead,
有幸看到我博客的朋友们,若能学到知识,请多多关注以及讨论,让我们共同进步,扬帆起航。
后记:打油诗一首
适度锻炼,量化指标
考量天气,设定目标
科学锻炼,成就体标
高效科研,实现学标