Iterator: a built-in tool for python-saving
nature Builder is an iterator
iterators and generators difference: a python is built, a write their own
A write generator:
DEF FUNC ():
Print (123)
return 'Hello'
FUNC ()
The yield is a return program generator
DEF FUNC ():
IF 2:
yield 'hello'
IF 1:
yield 'I'm good'
IF 3:
yield 'Hello everybody'
G = FUNC () # create a generator
print (g Next __ .__ ())
Print (G .__ Next __ ())
Print (G .__ Next __ ())
for i in g:
print(i)
def foo():
for i in range(10):
pass
yield i
count = 1
while True:
yield count
count += 1
g = foo()
for i in g:
print(i)
print(g.__next__())
print(g.__next__())
send () they know
Pit - generate new generator
Print (foo () Next .__ __ ())
Print (foo () Next .__ __ ())
solution
G = foo ()
Print (Next (G))
python2 not __next__
g.__iter__()
g.__next__()
By rewriting the function return is the yield in the generator
yield and return are returned
yeild recording execution location will
return one could write, but only once
a __next__ corresponds to a yield
generator may be used for loop to obtain the value
yield from - the by-element return iterables
send () to know
the internal functions, yield can for temporarily stopped and while loops
Builder application scenarios:
DEF FUNC ():
LST = []
for I in Range (100):
lst.append (I)
return LST
Print (FUNC ())
def func():
for i in range(100):
yield i
g = func()
for i in range(2):
print(next(g))
FUNC DEF ():
LST = [ 'cattle with', 'grandmother peanuts', 'Wei Long', 'shrimp Chedan', 'm old', 'Laoganma']
for I in LST:
the yield I
G FUNC = ()
Print (Next (G))
FUNC DEF ():
LST = [ 'cattle with', 'grandmother peanuts', 'Wei Long', 'shrimp Chedan', 'm old', 'Laoganma']
the yield from LST
G = FUNC ()
for i in g:
print(i)
推导式
lst = []
for i in range(20):
lst.append(i)
print(i)
list derivations
print ([i for i in range (20)])
Circulating derivation
[variables for i in range (20)]
筛选模式
lst = []
for i in range(20):
if i % 2 == 0:
lst.append(i)
print(lst)
Print ([I for I in Range (20 is)% 2 == 0 I IF])
[variables (variables after processing) for processing loop mode]
print([i for i in range(50) if i % 2 == 1])
print([i for i in range(1,50,2)])j
Generator expression
cyclic pattern
G = (I for I in Range (20 is))
Print (Next (G))
Print (Next (G))
Print (Next (G))
筛选模式
g = (i for i in range(50) if i % 2 == 1)
for i in g:
print(i)
Dictionary derivations (Learn)
Print ({I: I +. 1 for I in Range (20 is)})
Print ({I: I +. 1 for I in Range (20 is). 1 IF I%} == 2)
{Key: value, for loop processing conditions}
Set derivations
print ({i for i in range (20) if i% 2 == 1})
List
[variables (variables after processing) for loop]
[variables (variables after processing) for circulating the processing conditions]
Generator expressions
(variables (variables after processing) for loop)
(the variable (variable after processing) for circulating the processing conditions)
Dictionary derivation:
{key: value processing conditions for loop}
A set of derivation:
{variables (variables after processing) processing conditions for loop}
A built-in function module (not used)
S = "" "
for I in Range (10)
Print (I)
" ""
Print (Exec (S))
s1 = """
for i in range(10)
print(123)
func()
"""
print(exal(s))
print(exec(s1))
print (hash ('12 ')) hashing
print (help (list)) help view: Print source
FUNC DEF ():
Pass
Print (Callable ()) determines whether call, returns bool
print (float (2)) # Float
print (complex (56)) # complex
print (bin ()) transfected Binary Decimal
print (oct (15)) # Binary Octal
print (hex (15)) # Binary hex
print (divmod (5,2)) taken modulo quotient
print (round (5.3234,2)) rounded --- default integer, you can specify the number of decimal places
print (pow (2,3)) a power function
print (pow (2,3,4))
s= 'alex'
print(bytes(s.encode(s)))
print (ord ( 'you')) # the current encoding only a word a word to find .AECII
Print (CHR (20320)) to find the code word by bit
= S 'C: \ U3000'
Print (the repr (S)) is hit what what
print ( 'C: \ u3000 Hello')
= LST [1,2,3,4,5,0]
Print (All (LST)) determines whether a similar elements are true and
if there really similar or print (any (lst)) Analyzing element
. 1 = name
DEF FUNC ():
Print (123)
Print (Globals ()) # View global space variable
print (locals ()) # Check current space variable
Today's summary
1. generator
define a generator
function based on
the existence yield is a problem in the generator function
function name () is to create a generator
essence generator is a iterator
difference generators and iterators:
iterator is python It comes
generator is written by a programmer
generator:
space-saving mechanism inert ---
not retrograde
disposable
a next corresponding to a yield
yield can be returned content, but also a plurality of parameters return
yield cycle can be stopped snacks
yield can be recorded smothering execution position
yirld from - an iterable elements one by one return
2. derivation
List: [variables (variables after processing) for loop processing conditions]
dict: {key: value for loop processing conditions}
SET: {variables (variables after processing) processing conditions for loop}
generator expression: (variables (variables after processing) for circulating the processing conditions)
1. Derivation of different cyclic
2. Derivation Filter
Test derivation
Li = [I I * for I in Range (1,11)]
Print (Li)
li = [i*i for i in range(1,20) if i % 2 == 0]
print(li)
1-24 added to the list of
Li = [F "Python {I}" for I in Range (1,25)]
Print (Li)
The elements of the list is greater than 3 to stay
LST = [123,4,22,512,1,4,1,2,3,1,2,]
Print ([I I in for LST IF I> 3])
3.内置函数一
"""
all() any() bytes()
callable()
chr() complex()
divmod() eval()
exec() format()
frozenset() globals()
hash() help() id() input()
int() iter() locals()
next() oct() ord()
pow() repr() round()
"""