iterators: A preliminary study
As mentioned in the previous chapter, a for loop is actually available on any object that can be iterated. In fact, this is true for all of the iterative tools in Python that scan objects from left to right, including for loops, list parsing, in-member relationship testing, and map built-in functions.
The concept of an "iterative object" is quite novel in Python, basically this is the generalization of the concept of sequence: If an object is actually saved in a sequence, or can iterate over an object that produces a result once in the tool environment, it is considered to be iterative.
>> file iterators
A file that is a built-in data type is also iterative, and it has a method named __next__, which returns the next line in the file each time it is called. When the end of the file is reached, __next__ throws a built-in Stopiteration exception instead of returning an empty string.
This interface is the so-called iterative protocol in Python: an object with a __next__ method advances to the next result, and at the end of a series of results, it throws a stopiteration. Any such object is considered to be iterative. Any such object can also be traversed with a for loop or other iteration tool because all iteration tools work internally to call __next__ in each iteration and catch Stopiteratin exceptions to determine when to leave.
Python iterators and List parsing
1) iterators
A special data structure exists in the form of objects >>> i1 = l1.__iter__ () >>> i1 = iter (L1) can iterate over objects: sequence: list, str , tuple non-sequence: dict, File custom class: __iter__ (), __getitem__ () Note: to implement an iterator, you need to define the next () method in the class to make the iterator point to the next object, use the member function next () I1.next () when there are no elements, the stopiteration exception is thrown when the for loop can be used for any iteration object example: >>> l1 = [' Sun ', ' Mon ', ' Tue ', ' Wed ', ' Thu ', ' Fri ', ' Sat '] >>> i1 = l1.__iter__ () > >> il.next () ' Sun ' >>> il.next () ' Mon '
2) List parsing []
The way to produce a new list efficiently, based on an existing list. Often used to create a new list, so put it in [] syntax: [expression for Iter_var in iterable] [expression for Iter_var in iterable if Cond_ Expr] OS module listdir () can list all file examples: pre-defined L1 l2 l1 = [1,2,3,4,5] l2=[] >>> for i in l 1: l2.append (i**2) L2 list of L1 squares >>> print L2 [1, 4, 9, +] >>> L3 = [i**2 for i in L1] L3 list items are L1 squared [1, 4, 9, +,] >>> l4 = [i**2 for i in L1 if i>=3] L3 in the list of >= 3 squared in L1 [9, +/-] Example: l1 = [' x ', ' y ', ' z '] l2 = [x-ray] apply a list-parsing method to multiply its items by multiplying >>> L3 = [(i,j) for i-L1 for J in L2] for loop internally nested A For loop implementation of the Multiply [(' X ', 1), (' X ', 2), (' X ', 3), (' Y ', 1), (' Y ', 2), (' Y ', 3), (' Z ', 1), (' Z ', 2), (' Z ') ', 3)]
3) Generator ()
Lazy calculation, deferred evaluation generator expression does not really create a list of numbers, but instead returns a generator object that, after each calculation of an entry, generates the entry (return one value at a time) syntax: (expr for Iter_var in iterable) (expr for Iter_var in iterable if cond_expr) example: >>> l1 = (i**2 for i in range (2,11, 2)) >>> l1.next () 2 >>> l1 = (i**2 for i in range (2,11) if i%2==0) >>> L1.N Ext () 2 Note: when the sequence is too long and only one element is fetched at a time, you should consider using a builder expression instead of a list resolution list to resolve the relationship with the generator equivalent to range () and xrange ()
4) Create offsets and elements
Enumerate offset Both the zoom and offset elements, using the enumerate () function this built-in function returns a generator object >>> S = ' Hello,world ' >> > A = Enumerate (S) >>> a.next () (0, ' H ') >>> a.next () (1, ' e ')