Python built-in function reversed () __ function

Source: Internet
Author: User

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.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.