Outline of this section
- Iterators & Generators
- Decorative Device
- Basic Decorator
- Multi-parameter Adorner
- Recursive
- Algorithm Basics: Binary lookup, two-dimensional array conversion
- Regular expressions
- Common Module Learning
- Jobs: Calculator Development
- Implementation of subtraction and extension priority resolution
- 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:
- 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
- A value in the collection cannot be accessed randomly, and can only be accessed from beginning to end
- You can't go back halfway through the interview.
- 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