In the process of using Python, it is easy to confuse the following concepts of correlation:
- Container (Container)
- An iterative object (iterable)
- Iterators (Iterator)
- Generator (Generator)
- Builder expression
- {list, set, dict} analytic type
1. Container (Container)
A container is a data structure used to store elements, which supports the test of membership, where the container stores all the data in memory, and typical containers in Python are:
- List, deque, ...
- Set,frozesets, ...
- Dict, Defaultdict, Ordereddict, Counter, ...
- Tuple, Namedtuple, ...
- Str
Determines whether an object contains an element to determine if it is a container
>>>assert1inch[A]#lists>>>assert4 not inch[A]>>>assert1inch{A-i}#sets>>>assert4 not inch{A} >>>assert1inch(12,3)#tuples>>>assert4 not inch(A)
The dictionary container is judged by checking if a key is included
>>> d = {1:"Foo", 2:"Bar", 3:"Qux"}>>>assert1inchD>>>assert4inchDtraceback (most recent): File"<stdin>", Line 1,inch<module>Assertionerror>>>assert "Foo" inchDtraceback (most recent): File"<stdin>", Line 1,inch<module>Assertionerror
The string is determined by checking if a substring is included.
>>> s="foo"assert"F" in s assert"b" in straceback (most recent call last): " <stdin> " in <module>assertionerror
2. Can iterate objects
Given a list or tuple, we can iterate through for the list or tuple through a loop, which we call an iteration (iteration).
Many containers are iterative objects, and more objects are also iterative objects, such as open files,sockets and so on. But anything that can return an iterator can be called an iterative object, which may sound a bit confusing, but it doesn't matter, there is a very important difference between an iterative object and an iterator.
>>> x=[1,2,3] >>> y=iter ( x) >>> z=iter (x) >>> Next (y) 1>>> next (y) 2>>> next (z) 1>>> type (x) <class " list " >>>> type (y) << Span style= "COLOR: #0000ff" >class " list_ Iterator ";
X is an iterative object, iterative objects and containers is a popular term, not a specific data type, list is an iterative object, Dict is an iterative object, set is also an iterative object. Y and Z are two independent iterators that hold a state inside the iterator that records where the current iteration is located, so that the correct element is available for the next iteration. Iterators have a specific type of iterator, such as List_iterator, Set_iterator. An iterative object implements the __iter__ and __next__ methods (the next method in Python2, Python3 is the __next__ method), which correspond to the built-in functions ITER () and next (). The __iter__ method returns an iterator that iterates over the object itself, which makes him both an iterative object and an iterator.
3. Some iterative tools
3.1 Parallel iterations
>>> names=['Greg','GREG1','GREG2']>>> ages=[18,12,13]>>> forIinchRange (len (names)):Print(names[i]+' is'+str (ages[i])) GREG2 is13GREG2 is13GREG2 is13>>> forN,ainchZip (names,ages):Print(names[i]+' is'+str (ages[i])) GREG2 is13GREG2 is13GREG2 is13
3.2 Numbered iterations
List1 = [" this " " is " "" one "" Test " ] for in Enumerate (list1): print(index, item) 0 this 1 is 2 a 3 test
3.2 Flipping and sorting iterations
>>> Sorted ([2,6,3,1,5])[1, 2, 3, 5, 6]>>> Sorted ('hello,world!')['!',',','H','D','e','L','L','L','o','o','R','W']>>> List (Reversed ('hello,world!'))['!','D','L','R','o','W',',','o','L','L','e','H']>>>"'. Join (Reversed ('hello,world!'))'!dlrow,olleh'4. iterators (iterator)
It is a stateful object that can return the next value in the container when you call the next () method, and any object that implements the __next__ () (Next ()) method in Python2 is an iterator, and it does not matter how it is implemented.
#iterators, 1, have the ITER method. 2, there is next method#You can use Isinstance () to determine whether an object is a Iterable object:l=[1,2,3,4]d=iter (L)Print(d)#<list_iterator object at 0x000002c34b71c160>#list,tuple,dict,string:iterablePrint(Next (d))Print(Next (d))Print(Next (d))Print(Next (d))#For Loop Internal three things: 1, call to iterate object Iter method, return iterator object#2, constantly invoking the next method of the iterator object#3, Handling stopiteration, encountering exit#For i in [1,2,3,4]:#iter ([1,2,3,4]) fromCollectionsImportiterator,iterablePrint(Isinstance (2,list))#Falsel=[1,2,3,4]d=iter (L)Print(d)Print(Isinstance (l,list))Print(Isinstance (l,iterable))Print(Isinstance ({}, iterable))Print(Isinstance ('ABC', iterable))Print(Isinstance (x forXinchRange (10)) , iterable))Print(Isinstance (iterable))#False#Summary#all objects that can be used for a for loop are iterable types;#all objects that can be used for the next () function are iterator types, which represent a sequence of lazy computations;#collection data types such as list, Dict, str, and so on are iterable but not iterator, but a iterator object can be obtained through the ITER () function. #Python's for loop is essentially implemented by constantly invoking the next () function, for example:#first get the iterator object:it = iter ([1, 2, 3, 4, 5]) whileTrue:#Loop: Try:#get the next value:x =Next (IT)exceptStopiteration:#exit the Loop if you encounter Stopiteration Break
Iterators can be used to solve the problem of reusable space
classFab (object):def __init__(self, max): Self.max=Max SELF.N, SELF.A, self.b= 0, 0, 1def __iter__(self):return Selfdef __next__(self):ifSELF.N <Self.max:r=self.b self.a, self.b= self.b, SELF.A +self.b SELF.N= SELF.N + 1returnRRaisestopiteration () forKeyinchFab (5): Print(Key)#1#1#2#3#5
The Fab is both an iterative object (because it implements the __iter__ method) and an iterator (because the __next__ method is implemented). instance variables self. A and self.b users maintain the state inside the iterator. Do two things each time you call the next () method:
- Modify the state for the next call to the next () method
- Generates a return result for the current call
An iterator is like a lazy-loaded factory that returns a value when someone needs it, and waits for the next call when it is not called.
5. Get the sequence from the iterator
List construction method explicitly converts iterators to lists
classTestiterator:value=0def __next__(self): Self.value+=1ifSelf.value>10: RaisestopiterationreturnSelf.valuedef __iter__(self):returnSelfti=Testiterator ()Print(List (TI))#[1, 2, 3, 4, 5, 6, 7, 8, 9, ten]
Python: iterators