认识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,

有幸看到我博客的朋友们,若能学到知识,请多多关注以及讨论,让我们共同进步,扬帆起航。

后记:打油诗一首

适度锻炼,量化指标

考量天气,设定目标

科学锻炼,成就体标

高效科研,实现学标


Python3内置函数——reversed() = 翻转我的世界的更多相关文章

  1. python3内置函数大全(顺序排列)

    python3内置函数大全 内置函数 (1)abs(),   绝对值或复数的模 1 print(abs(-6))#>>>>6 (2)all() 接受一个迭代器,如果迭代器的所有 ...

  2. python3内置函数大全

    由于面试的时候有时候会问到python的几个基本内置函数,由于记不太清,就比较难受,于是呕心沥血总结了一下python3的基本内置函数 Github源码:        https://github. ...

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

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

  4. Python3内置函数、各数据类型(int/str/list/dict/set/tuple)的内置方法快速一览表

    Python3内置函数 https://www.runoob.com/python3/python3-built-in-functions.html int https://www.runoob.co ...

  5. python3内置函数详解

    内置函数 注:查看详细猛击这里 abs() 对传入参数取绝对值 bool() 对传入参数取布尔值, None, 0, "",[],{},() 这些参数传入bool后,返回False ...

  6. python 之 python3内置函数

    一. 简介 python内置了一系列的常用函数,以便于我们使用,python英文官方文档详细说明:点击查看, 为了方便查看,将内置函数的总结记录下来. 二. 使用说明 以下是Python3版本所有的内 ...

  7. python3 内置函数详解

    内置函数详解 abs(x) 返回数字的绝对值,参数可以是整数或浮点数,如果参数是复数,则返回其大小. # 如果参数是复数,则返回其大小. >>> abs(-25) 25 >&g ...

  8. Python3 内置函数补充匿名函数

    Python3 匿名函数 定义一个函数与变量的定义非常相似,对于有名函数,必须通过变量名访问 def func(x,y,z=1): return x+y+z print(func(1,2,3)) 匿名 ...

  9. python3 内置函数

    '''iter()和next()'''# lst = [1, 2, 3]# it = iter(lst)# print(it.__next__())# print(it.__next__())# pr ...

随机推荐

  1. win10 + ubuntu双系统详细安装过程

    由于搞深度学习,电脑跟不上,换了一台神舟战神Z8,于是装一个ubuntu双系统,没想到几乎花了一天,还花了80个软妹币找人帮忙,蓝瘦,现在写下来供大家参考: 不得不说,win10 + ubuntu双系 ...

  2. 记录entityframework生成的sql语句

    Interceptors (EF6.1 Onwards) Starting with EF6.1 you can register interceptors in the config file. I ...

  3. bzoj2588 Spoj10628. count on a tree

    题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始为0,即第一个 ...

  4. centos6.5环境搭建openvp服务器及windows客户端搭建及配置详解

    1.环境搭建 说明: vpn client 192.168.8.16/24 openvpn server: eth0: 192.168.8.41 eth1: 172.16.1.10 app serve ...

  5. centos6.5系统bash损坏之救援模式修复

    1.模拟bash被损坏的情况 # mv /bin/bash /tmp [root@localhost ~]# sync [root@localhost ~]# shutdown -r now 2.挂载 ...

  6. 解决Idea运行testng套件无testoutput文件夹问题

    说明:testNG的工程我是使用eclipse创建的,直接导入到idea中,运行test时不会生产test-output,只能在idea的控制台中查看运行结果,然后到处报告,经过不懈的百度终于找到怎么 ...

  7. [转]CentOS7 下安装svn

    1. 安装 centos(我这里使用的是CentOS7)下yum命令即可方便的完成安装 $ sudo yum install subversion 测试安装是否成功: $ svnserve --ver ...

  8. python接口自动化测试十九:函数

    # 函数 a = [1, 3, 6, 4, 85, 32, 46]print(sum(a)) # sum,求和函数 def add(): a = 1, b = 2, return a + bprint ...

  9. web中切图、快速切图与web雪碧图制作的方法

    声明: web小白的笔记,欢迎大神指点,联系QQ:1522025433. 工具:Photoshop 1.复制文字:点击文章工具后选择文字. 2.矩形选框工具 看信息 f8, 取消矩形选框 Ctrl+D ...

  10. python 全栈开发,Day61(库的操作,表的操作,数据类型,数据类型(2),完整性约束)

    昨日内容回顾 一.回顾 定义:mysql就是一个基于socket编写的C / S架构的软件 包含: ---服务端软件 - socket服务端 - 本地文件操作 - 解析指令(mysql语句) ---客 ...