One, iterator
Iterators are suitable for traversing some huge or infinite collections, such as several G files!
Features of iterators:
1, visitors do not care about the internal structure of the iterator, only need to continue to fetch the next content through the next () Method! 2, can not randomly access the collection, a value, can only be accessed from beginning to end! 3, access to half can not back! 4, easy to cycle relatively large data collection, saving memory!
1>>> name = ITER (['Jiac','Piter','Kuga']) # #创建一个迭代器2>>>name.__next__ ()3 'Jiac'4>>>name.__next__ ()5 'Piter'6>>>name.__next__ ()7 'Kuga'8>>>name.__next__ () # #当遍历完集合中的元素后! 9 Traceback (most recent):TenFile"<stdin>", line1,inch<module> One stopiteration A>>>
Second, generator
When a function is called, returns a
iterators, the function is called the Generator (generator), and if the function contains the yield syntax, the function becomes the generator!
1 Generator Cash_miney2 def Cash_money (ARG):3 whileArg >0: 4ARG-= - 5 yield - 6Print"here's the money! ") 7 8A = Cash_money ( -) 9 Print (Type (a))Ten # # Return iterator OnePrint (a.__next__ ()) # - APrint (a.__next__ ()) # - -Print (a.__next__ ()) # - -Print (a.__next__ ()) # - thePrint (a.__next__ ()) # - - print (a.__next__ ()) #执行时报错如下 after:(iteration) - Traceback (most recent): -File"f:/51python/day4/demo.py", line -,inch<module> + print (a.__next__ ()) -Stopiteration
Yield implements single-threaded asynchronous concurrency: A parallel effect in a serial program! )
1 #消费者2 def Consumer (name):3Print"%s prepare to eat buns! "%name)4 whileTrue:5b =yield# The value of the #接收send () method6Print"steamed bun [%s] come on! Was eaten by [%s]"%(b,name))7 8 #生产者9 def producer (name):TenC1 = Consumer ("A") OneC2 = Consumer ("B") A c1.__next__ () # #输出yield的返回值 - c2.__next__ () # #输出yield的返回值 -Print"Lao Tzu began to prepare steamed buns! ") the forIinchRangeTen): -Time.sleep (1) -Print"2 buns have been made! ") - C1.send (i) # #把i的值发送给yield + C2.send (i) # #把i的值发送给yield - +Producer"Me") A at # # #打印输出如下: # # # - a prepare to eat buns! - b prepare to eat buns! - Lao Tzu began to prepare steamed buns! - 2 buns have been made! -Buns0] Come on! was eaten by [A] inBuns0] Come on! was eaten by [B] - 2 buns have been made! toBuns1] Come on! was eaten by [A] +Buns1] Come on! was eaten by [B] - 2 buns have been made! theBuns2] Come on! was eaten by [A] *Buns2] Come on! was eaten by [B] $ 2 buns have been made! Panax NotoginsengBuns3] Come on! was eaten by [A] -Buns3] Come on! was eaten by [B] the 2 buns have been made! +Buns4] Come on! was eaten by [A] ABuns4] Come on! was eaten by [B] the 2 buns have been made! +Buns5] Come on! was eaten by [A] -Buns5] Come on! was eaten by [B] $ 2 buns have been made! $Buns6] Come on! was eaten by [A] -Buns6] Come on! was eaten by [B] - 2 buns have been made! theBuns7] Come on! was eaten by [A] -Buns7] Come on! was eaten by [B]Wuyi 2 buns have been made! theBuns8] Come on! was eaten by [A] -Buns8] Come on! was eaten by [B] Wu 2 buns have been made! -Buns9] Come on! was eaten by [A] AboutBuns9] Come on! was eaten by [B]
Three, the decoration device
Four, the regular expression
4th-day iterators, generators, adorners, regular expressions