To figure out what an iterator is, first understand several nouns: a container (container), an iteration (iteration), an iterative object (iterable), an iterator (iterator), a generator (generator).
Look at the picture is not more clear point ...
container (Container)
A container is a data structure that organizes multiple elements together, and the elements in a container can be iterated over and over, and keywords can be used in
to not in
determine whether an element is contained in a container. Usually such data structures store all the elements in memory (there are some exceptions, not all elements are placed in memory, such as iterators and generator objects) in Python, common container objects are:
- List, deque, ....
- Set, Frozensets, ....
- Dict, Defaultdict, Ordereddict, Counter, ....
- Tuple, Namedtuple, ...
- Str
The container is easier to understand because you can think of it as a box, a house, a cupboard, which can be stuffed with anything. Technically, when it can be used to ask if an element is contained in it, the object can be thought of as a container, such as a list,set,tuples is a container object.
Print (1 in [1, 2, 3]) # lists# Trueprint (4 No in [1, 2, 3]) # Trueprint (1 in {1, 2, 3}) # sets# Trueprint (4 not I n {1, 2, 3}) # Trueprint (1 in (1, 2, 3)) # tuples# Trueprint (4 No in (1, 2, 3)) # true# asks if an element is in dict with dict = {1: ' Foo ', 2: ' Bar ', 3: ' Qux '}print (1 in D) # trueprint (' foo ' isn't in D) # ' Foo ' is not an element in Dict # true# asks if a substring is in string: s = ' foobar ' Print (' B ' in s) # Trueprint (' x ' isn't in s) # trueprint (' foo ' in s) # True
Although the vast majority of containers provide some way to get each of these elements, this is not the ability provided by the container itself, but the ability to iterate over objects to the container, not all of which are iterative, for example:Bloom filter, Although Bloom filter can be used to detect whether an element is contained in a container, it is not possible to get each of these values in a single device, because Bloom Filter does not store the element in the container at all, but instead maps it to a value in the array by a hash function.
Iteration (Iteration)
What is the iteration, I understand as follows:
First, iterations need to repeat an action
Second, this iteration relies on the last result to continue, and if there is any pause in the middle, it is not an iterative
Here are a few examples to better understand the meaning of iterations.
# instance # non-iterative count = 0while count <: print ("Hello World") count + = 1 # instance * Iteration count = 0while Count < 10: print (count) count + = 1
Example 1, just repeating one thing, is to print "Hello World" all the time, and the result of this printing does not depend on the value of the last output. and Example 2, it is good to explain the meaning of the iteration, repeat + continue.
an Iterative object (iterable)
The popular saying is that in each data type object, there is a __iter__ () method, precisely because of this method, so that these basic data types become iterative.
When we run the following code:
x = [1,2,3]for elem in x: print (elem) # Run Result: # 3
The actual invocation process is as follows:
So how can you tell if an object is iterative? Use the iterable type of the collections module to determine
From collections Import Iterableprint (Isinstance (' abc ', iterable)) # str can iterate # trueprint (Isinstance ([], iterable) # whether the list can iterate # Trueprint (Isinstance (123, iterable)) # integer can iterate # False
iterators (iterator)
In layman's terms, any object that has a __next__()
method is an iterator that calls the __next__ () method on the iterator to get the next value.
Generator (Generator)
The generator is a simple way to complete an iteration. In simple terms, the Python builder is a function that returns an object that can be iterated.
So how to create a generator, very simple, in the general function of the use of yield
keywords, you can implement a simplest generator, this function becomes a generator function. The same value as the return, the difference is that after returning, the function state terminates, and the execution state of the current function is saved, and after returning, the function goes back to the previously saved state to continue execution. return
yield
return
yield
Take a look at a simple generator instance:
def test (): yield 1 yield 2 yield 3g=test () print (' From function ', g) print (g.__next__ ()) print (g.__next__ ()) # Run result # from Function <generator Object Test at 0x000000000072b8e0># # 2
What is the difference between a generator and a general function?
- The generator function contains one or more
yield
- When the generator function is called, the function returns an object, but does not immediately execute down
- Like
__iter__()
and __next__()
methods are implemented automatically, so we can next()
iterate over objects by means of methods
- Once the function is stopped, the
yield
function is paused and control returns to the caller
- Local variables and their states are saved until the next call
- When the function terminates,
StopIteraion
it is automatically thrown.
Let's take a look at the example:
# Simple generator function Def my_gen (): n=1 Print ("First") # Yield Area yield n n+=1 print ("second") Yield n n+=1 print ("third") yield Na=my_gen () print ("Next Method:") # each time A is called, the function executes from the previously saved state printprint ( Next (a)) (Next (a)) print (Next (a)) # Run result # next method:# first# # second# * third# 3print ("For Loop:") # with the call next equivalent B=my_gen ( ) for Elem in My_gen (): print (elem) # Run result # for loop:# first# # second# 3
Take a look at the generator using loops
# The element of the reverse yield object def rev_str (my_str): Length=len (MY_STR) for I in range (length-1,-1,-1): yield my_str[i ]for Char in Rev_str ("Hello"): print (char) # run result # o# l# l# e# H
Builder Expression
Python, there is a list generation method, which is often said list parsing, referring to list parsing first to understand the concept of ternary expression, what is ternary expression? Let's take an example.
Egg_list=[]for i in range: egg_list.append (' egg%s '%i) print (egg_list) # [' Egg 0 ', ' egg 1 ', ' Egg 2 ', ' Egg 3 ', ' Egg 4 ', ' Egg 5 ', ' Egg 6 ', ' Egg 7 ', ' Egg 8 ', ' Egg 9 ']# use ternary expression Replace as above code l=[' egg%s '%i for I in range (Ten)]print (l) # [' Egg 0 ', ' egg 1 ', ' Egg 2 ', ' Egg 3 ', ' Egg 4 ', ' chicken ' Egg 5 ', ' Egg 6 ', ' Egg 7 ', ' Egg 8 ', ' Egg 9 ']l1=[' egg%s '%i for I in range () If I > 5]print (L1) # [' Egg 6 ', ' Egg 7 ', ' Egg 8 ', ' Egg 9 ']# l2= [' Egg%s '%i for I in range ' If I > 5 else i] #没有四元表达式 # print (L2) l3=[' egg%s '%i for I in range (Ten) if I < 5] Print (l 3 # [' Egg 0 ', ' egg 1 ', ' Egg 2 ', ' Egg 3 ', ' egg 4 ']
Understanding the ternary expression, we look at what is the generator expression, in fact, is very simple, is the ternary expression in the [] can be replaced by ().
Python3 iterators and generators