The reversed () function is the iterative child that returns the reverse access of the sequence seq. Parameters can be lists, tuples, strings, and do not change the original object.
1 "parameter is a list
>>> l=[1,2,3,4,5]
>>> ll=reversed (L)
>>> L
[1, 2, 3, 4, 5]
>>> LL
<listreverseiterator Object at 0x06a9e930>
>>> for I in LL: #第一次遍历
... print I,
...
5 4 3 2 1
>>> for I in LL: The second traversal is empty, cause see the end of this article
... print I
...
2 "parameter is a list
>>> l=[3,4,5,6]
>>> ll=reversed (L)
>>> L
[3, 4, 5, 6]
>>> LL
<listreverseiterator Object at 0x06a07e10>
>>> List (LL) #第一次
[6, 5, 4, 3]
>>> List (ll) #第二次为空, for reasons see the end of this article
[]
3 parameters are tuples
>>> t= (4,5,6)
>>> tt=reversed (t)
>>> T
(4, 5, 6)
>>> TT
<reversed Object at 0x06a07e50>
>>> tuple (TT) #第一次
(6, 5, 4)
>>> tuple (TT) #第二次为空, for reasons see the end of this article
()
4 "parameter is a string
>>> s= ' CBA '
>>> ss=reversed (s)
>>> s
' CBA '
>>> SS
<reversed Object at 0x06a07e70>
>>> List (ss) #第一次
[' A ', ' B ', ' C ']
>>> List (ss) #第二次为空, for reasons see the end of this article
[]
5 "parameter is a string
>>> s= ' 1234 '
>>> ss=reversed (s)
>>> s
' 1234 '
>>> SS
<reversed Object at 0x06a94490>
>>> '. Join (SS) #第一次
' 4321 '
>>> '. Join (SS) #第二次为空 for reasons see the end of this article
''
Why after reversed (), the result of the second for loop or second list () or second tuple () or second join () is empty. Let us specify in the 2nd example:
That ' s because reversed creates a iterator, which is already spent while you ' re calling list (ll) for the second time.
The reason is the reversed list itself, but a listreverseiterator. So while you are call list (LL) the the "I", it iterates over ll and creates a new list from the the items output from that Itera Tor. When doing it a second time, ll are still the original iterator and has already gone all of the items, so it through ' t Iterate over anything, resulting in a empty list.
Summary: After reversed (), the value is returned only in the first time of the calendar.