更新时间:2023-11-14 12:27:28
在Python中, reversed
实际上返回一个反向迭代器.因此,应用于迭代器的 list
会为您提供列出对象.
In Python, reversed
actually returns a reverse iterator. So, list
applied on the iterator will give you the list object.
在第一种情况下,输入也是一个列表,因此将list
的结果应用于reversed
迭代器似乎对您来说合适.
In the first case, input was also a list, so the result of list
applied on the reversed
iterator seemed appropriate to you.
在第二种情况下, str
应用于返回的迭代器对象实际上将为您提供它的字符串表示形式.
In the second case, str
applied on the returned iterator object will actually give you the string representation of it.
Instead, you need to iterate the values in the iterator and join them all with str.join
function, like this
>>> ''.join(reversed('abcde'))
edcba