#Generator: The emergence of the generator is to avoid the problem of quickly generating a batch of data and occupying too much memory in an instant. #The way to use is when you need data, go to the generator to get it, and then open up space in the memory #List generation is to quickly generate a list and open up the corresponding space in memory #Format 1: Replace [] in the list with () g=(x for x in range ( 100 )) #Get data next() print ( next (g)) #If the generator has no data, it will report an error li=[ 1 , 3 , 5 , 6 , 9 ] for x in g: if x== 10 : break print (x) # Format 2: Get the specified number of data # function combined with yield def get_value(num): for i in range (num): yield i #Return the data #print(i) #The next time you get it, execute print, you can do without gl=get_value( 10 ) #Get the data, you can do it one by one You can also use forin print ( "value just obtained:" , next (gl)) print ( "value just obtained: " , next (gl)) for i in gl: print (i)
generator concept
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=324723135&siteId=291194637
Ranking