Python Note 4 (Iterators and Generators)

1. Iterator

How to get a value from a list or a dictionary:
1) index index, key, the premise must know the index or key value to get the corresponding value
2) for loop
Anything that can use the for loop to get the value is an iterable
iterable protocol : Internally containing __iter__ methods are iterable
iterator protocols: Internally containing __iter__ methods and __next__ methods are iterators

 1 print(dir([1,2,3]))
 2 #>>>['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
 3 lst_iter = [1,2,3].__iter__()
 4 print(lst_iter.__next__ ()) # >>>1 
5  print (lst_iter. __next__ ()) # >>>2 
6  print (lst_iter. __next__ ()) # >>>3 
7  
8  #The above code is equivalent to the for loop value 
9  for i in [1,2,3]:    #When   using a for loop, the __iter__ method is called internally to become an iterator. [1,2,3].__iter__() 
10      print (i)
 11  
12  #The mechanism of using while loop to implement for loop 
13 l = [1,2,3 ]
 14 lst_iter = iter(l)    # iter(l) is equivalent For l.__iter__(), both generate an iterator 
15 while True:
 16      try :
 17          print (next(lst_iter)) # next(lst_iter) is equivalent to lst_iter.__next__() 
18      except StopIteration:
 19          break 
20  
21  #what is iterable 
22  #what is iterator iterator = iter (Iterable), comes with a __next__ method 
23  #The biggest advantage of iterable saves memory 
24  
25  #Example : The following is the result of python3 running 
26  from collections import Iterable,Iterator
 27  print (range(100000000 ))
 28  print (isinstance(range(100000000),Iterable))#Judging whether it is iterable 
29  print (isinstance(range(100000000),Iterator)) #Judging whether it is an iterator 
30  # >>>range(0, 100000000) 
31  # >>>True 
32  # >>>False 
33  
34  # python2 range will generate a list regardless of the range and this list will be used to store all the values 
​​35  # python3 range will not actually generate any value regardless of the range 
36  # advantages of iterators: 
37  #      save memory 
38  #      take A value can be used for the next calculation without waiting for all the values ​​to be calculated before starting the next calculation - fast 
39  #Iterator characteristics: lazy calculation 
40  
41  #Example 42 
f =open()
 43  for line in f: #Read a line with one line 
44  
45  #Iterable object: list dictionary tuple string collection range file handle enumerate (plus serial number)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324928055&siteId=291194637