Use pyhton ternary generator

# Ternary iteration 

List1 = [1,2,3,4]
# List List
A = [I for I in List1]
# Generator Generator (simple point is appreciated that a generator iterator)
'' '
benefits generator :
1. small memory, and the like do not need to be loaded like a list of all the elements in the memory into iterator
2. high efficiency, no conversion to iterator
'' '
# + three yuan generator
b = (i for i in List1 IF I> 2)
for I in B:
Print (I)
# iterator
C = List1 .__ ITER __ ()
Print (type (a))
Print (type (B))
Print (type (C))

# generator use
DEF Genl ():
the yield. 1
the yield 2
the yield. 3
Gen = Genl ()
Print (Next (Gen))
Print (Next (Gen))
Print (Next (Gen))

DEF Gen2 (name):
the while True:
# Res accepts values gen.send (obj) pass (Note: name and pass no necessary relationship parameter)
RES = the yield "name: S%"% name
name = RES
Gen = Gen2 ( "LDS")
Print (Gen .send (None))
Print (gen.send ( "123"))
Print (gen.send ( "1234"))

Guess you like

Origin www.cnblogs.com/lides/p/11080070.html