Python built-in functions (54) -- reversed, pythonreversed

Source: Internet
Author: User

Python built-in functions (54) -- reversed, pythonreversed

English document:

reversed( Seq)
Return a reverse iterator. SeqMust be an object which has __reversed__()Method or supports the sequence protocol ( __len__()Method and __getitem__()Method with integer arguments starting 0).

Note:

1. The function is to reverse a sequence object and reverse its elements from the back to the front to form a new iterator.

>>> A = reversed (range (10) # input range object >>> a # type becomes iterator <range_iterator object at 0x035634E8 >>> list () [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]> a = ['A', 'B', 'C ', 'D'] >>> a ['A', 'B', 'C', 'D'] >>> reversed () # input list object <list_reverseiterator object at 0x031874D0 >>> B = reversed (a) >>> B # type conversion to iterator <list_reverseiterator object at 0x037C4EB0> list (B) ['D', 'C', 'B', 'a']

 

2. If a parameter is not a sequence object, it must define_ Reversed _ method.

# Type Student does not define _ reversed _ method> class Student: def _ init _ (self, name, * args): self. name = name self. scores = [] for value in args: self. scores. append (value) >>> a = Student ('bob', 78,85, 93,96) >>> reversed (a) # The instance cannot reverse Traceback (most recent call last ): file "<pyshell #37>", line 1, in <module> reversed (a) TypeError: argument to reversed () must be a sequence >>> type (. scores) # list type <class 'LIST'> # redefine the type and define _ reversed _ method for it> class Student: def _ init _ (self, name, * args): self. name = name self. scores = [] for value in args: self. scores. append (value) def _ reversed _ (self): self. scores = reversed (self. scores) >>> a = Student ('bob', 78,85, 93,96) >>>. scores # list type [78, 85, 93, 96] >>> type (. scores) <class 'LIST' >>> reversed (a) # The instance can be reversed >>>. scores # After reversal, the type is changed to the iterator <list_reverseiterator object at 0x0342F3B0> type (. scores) <class 'list _ reverseiterator '> list (. scores) [96, 93, 85, 78]

 

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.