Python iterators & Generators, adorners, recursion, Algorithm basics: Binary lookup, two-dimensional array conversion, regular expressions, jobs: Calculator Development

Source: Internet
Author: User

Outline of this section
    1. Iterators & Generators
    2. Decorative Device
      1. Basic Decorator
      2. Multi-parameter Adorner
    3. Recursive
    4. Algorithm Basics: Binary lookup, two-dimensional array conversion
    5. Regular expressions
    6. Common Module Learning
    7. Jobs: Calculator Development
      1. Implementation of subtraction and extension priority resolution
      2. User Input 1-2 * ((60-30 + ( -40/5) * (9-2*5/3 + 7/3*99/4*2998 +10 * 568/14))-( -4*3)/(16-3*2)) and other similar formulas, you must parse the inside (), +,-, *,/character Numbers and formulas, and the results must be consistent with the results of the actual calculator.
Iterators & Generators

Iterators

Iterators are a way to access the elements of a collection. The iterator object is accessed from the first element of the collection until all of the elements have been accessed and finished. Iterators can only move forward without going backwards, but that's fine, because people seldom retreat in the middle of an iteration. In addition, one of the great advantages of iterators is that they do not require that all elements in the entire iteration be prepared in advance. An iterator computes an element only when it iterates over it, and before or after that, the element may not exist or be destroyed. This feature makes it ideal for traversing large or infinite collections, such as several G files

Characteristics:

    1. The visitor does not need to care about the structure inside the iterator, but simply continues to fetch the next content through the next () method
    2. A value in the collection cannot be accessed randomly, and can only be accessed from beginning to end
    3. You can't go back halfway through the interview.
    4. Facilitates recycling of large data sets, saving memory

To generate an iterator:

>>> a = ITER ([1,2,3,4,5]) >>> A<list_iterator object at 0x101402630>>>> a.__next__ () >>> a.__next__ () >>> a.__next__ () >>> a.__next__ () >>> a.__next__ () >>> A. __next__ () Traceback (most recent):  File ' <stdin> ', line 1, in <module>stopiteration

Call Iterator's __next__ () method repeatedly (or pass it to the built-in function next ()) to return successive entries in the stream. when no more data is available, a stop iteration exception is raised. at this point, the iterator object is exhausted and any further calls to its __next__ () method will again raise the stop iteration.

In Python, many objects can be traversed directly through a for statement, such as list, String, Dict, and so on, which can be called an iterative object. As for which objects can be accessed iteratively, it is necessary to understand the knowledge of iterators.

Iterators
The iterator object requires an object that supports the iterator protocol, in Python, the __iter__ () and Next () methods that implement the object are the support iterator protocol. where the __iter__ () method returns the Iterator object itself; the next () method returns the next element of the container and throws an Stopiteration exception at the end.

__iter__ () and Next () methods
These two methods are the most basic method of iterators, one to get the iterator object, and one to get the next element in the container.
For an iterative object, you can use the built-in function iter () to get its iterator object:

example, the list iterator object is obtained through the ITER () method, and then the elements in the list can be accessed through the next () method. When there are no accessible elements in the container, the next () method throws an Stopiteration abort iterator.
In fact, when we use the For statement, the For statement automatically obtains the iterator object through the __iter__ () method, and gets the next element through the next () method.

Python iterators & Generators, adorners, recursion, Algorithm basics: Binary lookup, two-dimensional array conversion, regular expressions, jobs: Calculator Development

Related Article

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.