import numpy as np
'' '
Iteration: The results obtained are based on each result of the last (replacement)
iterator protocol: next object must provide a method for performing the method, or a return to the iterator, or cause abnormal
iterables (saving memory): implement the iterator protocol object (internal: __ ITER __ () method)
for circulating nature: circulating all the objects, all the iterator protocol
for loop can iterate (SEQ [strings, lists, ancestral], non-sequential [dictionary, a collection of file objects])
'' ' '
''
Q & a :( strings, lists, tuples, dictionaries, collections, file objects) are not iterables,
just for the cycle call their internal __iter__ ways to bring them into iterables
' ''
= X ' Hello ' Print (the dir (X)) # # view corresponding method iter_test = X. the __iter__ () # ## Strings to iterator object Print (iter_test) # # <Object str_iterator AT 0x000001FE5A566550> iterator object memory address Print (iter_test. __next__ ()) Print (iter_test. __next__ ()) Print (iter_test. __next__ ()) Print (iter_test. __next__ ()) Print (iter_test. __next__ ()) # Print (iter_test .__ the Next __ ( )) '' ' Summary: string object for loop (internal analysis): 1. First the __iter __ () is converted into iterables 2. And then the __next __ () iterates (God - parent - child) 3. Automatic catch exceptions ' '' # access (strings, lists, tuples) ## sequence class LIS = [0,1,2,3,4 ] for I in LIS: Print (I) # # iterator protocol values (first iterator object becomes ) . iter_lis = LIS the __iter__ () Print (iter_lis. __next__ ()) # # traversing the index index = 0 the while index < len (LIS): Print (LIS [I]) index + =. 1 # ## non-sequence type (Dictionary , a collection of document objects) # set S = {l, 2,3 } for i in s: print(i) #解析 iter_s = s.__iter__() print(iter_s) print(iter_s.__next__()) print(iter_s.__next__()) print(iter_s.__next__()) ##字典 dic = {'a':1,'b':2} iter_dic = dic.__iter__() print(iter_dic.__next__()) ###取出的是key ## File object (create a file in the working directory) F = Open ( ' test.txt ' , ' R & lt + ' , encoding = ' UTF-. 8 ' ) # for I in F: # Print (I) iter_f = F. The __iter__ () Print (iter_f. __next__ (), End = ' ' ) Print (iter_f. __next__ (), End = ' ' ) Print (iter_f. __next__ (), End = ' ' ) Print (iter_f. __next__ (), End =' ' ) # #Next () may also become the target iteration object, calls the Next __ .__ () DIC1 = { ' A ' :. 1, ' B ' : 2, ' C ' :. 3 } iter_dic1 = DIC1. The __iter__ () Print (the Next (iter_dic1)) Print (the Next (iter_dic1)) Print (the Next (iter_dic1))
Generating characteristic: [iterables]
1. automatically iterator protocol
2. The generator function: yield statement: time returns a result, the state remains, then the next executed (every step, pause, and then go)
3. List of parsing a triplet of expressions
4. Generate expressions:
'' '
# # Ternary expressions with name = ' Alex ' RES1 = ' before true ' IF name == ' Alex ' the else ' after false ' RES2 = ' before true ' IF name == ' Lex ' the else ' after false ' Print (RES1 , RES2) '' ' if it is determined [true], res =' before true ' if it is determined [false], res =' after false ' ' '' # # Analytical listing # two yuan LIS = [I for I in Range (10 )] Print(LIS) # three yuan LIS1 = [ ' eggs S% ' % I for I in Range (10) IF I>. 5 ] Print (LIS1)
Builder: a generator expression (Analytical list [] with ()) yield statement generator function 2
# # 1. Generator expressions (resolved list [] with ()), more economical with respect to memory the list of parsed laomuji = ( ' eggs S% ' % I for I in Range (10 )) Print (laomuji) # <Object Generator <genexpr> AT 0x00000218948C2318> Print (laomuji. __next__ ()) Print (laomuji. __next__ ()) Print (laomuji. __next__ ()) Print (Next (laomuji)) Print (SUM (X ** 2 for X in Range (. 4))) # # generator expression calculated directly Print (SUM ([X ** 2 for Xin Range (. 4)])) # # generator expression generate a list, and then calculating # # 2 generator function. DEF GenerateData (BatchSize = 100 ): train_x = np.linspace (-1,1, BatchSize) # # # arithmetic sequence (one-dimensional array) train_y = 2 * + np.random.randn train_x (train_x.shape *) 0.3 * # ## was added noise y = zx (one-dimensional array) # # np.random.randn ( shape) generated between 0-1, including 0 but excluding random number of the yield train_x, train_y # # to return to the training data generator manner X, Y for I in Range (. 5 ): for X, Y in GenerateData (): Print (X [:. 3 ]) Print(And [3])