Why str(reversed(…)) doesn't give me the reversed string?

前端 未结 2 2105
情书的邮戳
情书的邮戳 2021-01-04 20:12

I\'m trying to get used to iterators. Why if I type

b = list(reversed([1,2,3,4,5]))

It will give me a reversed list, but

c          


        
2条回答
  •  半阙折子戏
    2021-01-04 20:50

    another way by extend slice method. more details

    >>> a = "abcde"
    >>> a[::-1]
    'edcba'
    >>> 
    

    by string to list --> list reverse --> join list

    >>> a
    'abcde'
    >>> b = list(a)
    >>> b
    ['a', 'b', 'c', 'd', 'e']
    >>> b.reverse()
    >>> b
    ['e', 'd', 'c', 'b', 'a']
    >>> "".join(b)
    'edcba'
    >>> 
    

提交回复
热议问题