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).
The constructor builds a list whose items are the same and in the same
order as iterable’s items. iterable may be either a sequence, a
container that supports iteration, or an iterator object. If iterable
is already a list, a copy is made and returned, similar to
iterable[:]. For example, list('abc') returns ['a', 'b', 'c'] and
list( (1, 2, 3) ) returns [1, 2, 3]. If no argument is given, the
constructor creates a new empty list, [].
如果要反转字符串,请尝试以下操作:
这是因为reversed输出一个迭代器,迭代器不保存值,而是动态计算值。“”连接部分使用生成器并生成字符串。你知道吗
有关生成器/迭代器的详细信息,请参见here。你知道吗
从Python文档中:
reversed(seq)
list([iterable])
所以:
reversed(string)
将返回一个遍历字符串的迭代器。list(iterator)
将迭代器转换为一个列表。list(string)
将字符串拆分为其组成字符。你知道吗因此
list('india')
返回['i', 'n', 'd', 'i', 'a']
reversed('india')
返回<reversed object at 0x1090b48d0>
,这是一个迭代器可以使用
list
运算符将迭代器转换为列表:list(reversed('india'))
返回['a', 'i', 'd', 'n', 'i']
或者你可以迭代:
打印出来的
相关问题 更多 >
编程相关推荐