标签:
一、迭代器&生成器特点:
#!/usr/bin/env python
# -*-coding:utf-8 -*-
names = iter([‘feng‘,‘soft‘,‘worter‘])
print(names)
print(names.__next__())
print(names.__next__())
print(names.__next__())
#print(names.__next__())
<list_iterator object at 0x00000000021CB208>
feng
soft
worter
#!/usr/bin/env python
# -*-coding:utf-8 -*-
names = iter([‘feng‘,‘soft‘,‘worter‘])
print(names)
print(names.__next__())
print(names.__next__())
print(names.__next__())
print(names.__next__())
<list_iterator object at 0x00000000027BB438>
Traceback (most recent call last):
feng
File "E:/Python/S12/day4/didaiqi.py", line 8, in <module>
soft
print(names.__next__())
worter
StopIteration 看见这个报错
Process finished with exit code 1
作用:yield可以使函数中断,并保存中断状态,中断后,代码可以继续往下执行,过一段时间还可以再重新调用这个函数,从上次yield的下一句开始执行。
#!/usr/bin/env python
# -*-coding:utf-8 -*-
def cash_money(amount):
while amount > 0 :
amount -= 100
yield 100
print("过来取钱了啦")
atm = cash_money(500)
print(type(atm))
print(atm.__next__())
print(atm.__next__())
print(‘testset‘)
print(atm.__next__())
<class ‘generator‘>
100
过来取钱了啦
100
testset
过来取钱了啦
100
Process finished with exit code 0
#!/usr/bin/env python
# -*-coding:utf-8 -*-
import time
def consumer(name): # 消费者
print(‘%s 准备吃包子了!‘ %name)
while True:
baozi = yield
print("包子[%s]来了,被[%s]吃了!" %(baozi,name))
def producer(name): #供方
c = consumer(‘zhangs‘)
d = consumer(‘li‘)
c.__next__()
d.__next__()
print(‘顾客要吃包子,开始做包子啦!‘)
for i in range(10): #range 0-9
time.sleep(1) #程序停止1秒
print(‘做了两个!‘)
c.send(i) #和前面的yield进行配合。
d.send(i)
producer(‘feng‘)
zhangs 准备吃包子了!
li 准备吃包子了!
顾客要吃包子,开始做包子啦!
做了两个!
包子[0]来了,被[zhangs]吃了!
包子[0]来了,被[li]吃了!
做了两个!
包子[1]来了,被[zhangs]吃了!
包子[1]来了,被[li]吃了!
做了两个!
包子[2]来了,被[zhangs]吃了!
包子[2]来了,被[li]吃了!
做了两个!
包子[3]来了,被[zhangs]吃了!
包子[3]来了,被[li]吃了!
做了两个!
包子[4]来了,被[zhangs]吃了!
包子[4]来了,被[li]吃了!
做了两个!
包子[5]来了,被[zhangs]吃了!
包子[5]来了,被[li]吃了!
做了两个!
包子[6]来了,被[zhangs]吃了!
包子[6]来了,被[li]吃了!
做了两个!
包子[7]来了,被[zhangs]吃了!
包子[7]来了,被[li]吃了!
做了两个!
包子[8]来了,被[zhangs]吃了!
包子[8]来了,被[li]吃了!
做了两个!
包子[9]来了,被[zhangs]吃了!
包子[9]来了,被[li]吃了!
def login(func1): #这个时候的(func1)参数为(tv(‘func2‘))
def inner(arg):
print(‘passswd user verification.........‘)
func1(arg) #arg等价于func2
# return func()
return inner #此处的inner为一个函数体 #编号1
@login #等价于 login(tv)
def tv(name):
print(‘welcome to tv‘)
tv(‘func2‘)
C:\Python34\python.exe E:/Python/zhuangshiqi.py
passswd user verification.........
welcome to tv
login(tv(‘func2‘)): #这个时候的(func1)参数为(tv(‘func2‘))
def inner(arg):
print(‘passswd user verification.........‘)
func1(arg)
# return func()
return inner #此处的inner为一个函数体 #编号1
def w1(func): def inner(arg): # 验证1# 验证2# 验证3return func(arg) return inner @w1 def f1(arg): print ‘f1‘
两个参数的:
def w1(func): def inner(arg1,arg2): # 验证1# 验证2# 验证3return func(arg1,arg2) return inner @w1 def f1(arg1,arg2): print ‘f1
三个参数的:
def w1(func): def inner(arg1,arg2,arg3): # 验证1# 验证2# 验证3return func(arg1,arg2,arg3) return inner @w1 def f1(arg1,arg2,arg3): print ‘f1‘下面这个重要:装饰N个参数的。
def w1(func1):
def inner1(*args,**kwargs):
print(‘ONE‘)
return func1(*args,**kwargs)
return inner1
def w2(func2):
def inner2(*args,**kwargs):
print(‘TWO‘)
return func2(*args,**kwargs)
return inner2
@w1
@w2
def f1(arg1,arg2,arg3):
print(‘tset222‘)
return f1
test = f1(‘one‘,‘two‘,‘three‘)
print(test)
执行结果
C:\Python34\python.exe E:/Python/S12/day4/zhuangshiqi.py
ONE
TWO
tset222
<function w1.<locals>.inner1 at 0x00000000021FCC80> (这个啥意思?)
执行过程是先执行@w1,在执行@w2,,先顺序的对@w1、@w2第一层执行一遍,然后再一次执行第二层,执行完第二层后会再次跳到@w1,利用刚刚赋值的参数,去执行f1里面的参数。一个函数被多个装饰器装饰
def binary_search(data_source,find_n):
mid = int(len(data_source)/2)
if len(data_source) >1:
if data_source[mid] > find_n:
print(‘data in left of [%s]‘ % data_source[mid])
binary_search(data_source[:mid],find_n)
elif data_source[mid] < find_n:
print(‘data in right of [%s]‘ % data_source[mid])
binary_search(data_source[mid:],find_n)
else:
print(‘found‘,data_source[mid])
else:
print(‘cannot find ...‘)
if __name__ == ‘__main__‘:
data = list(range(1,6000000))
binary_search(data,666666)
C:\Python34\python.exe E:/Python/递归——list.py
data in left of [3000000]
data in left of [1500000]
data in left of [750000]
data in right of [375000]
data in right of [562500]
data in right of [656250]
data in left of [703125]
data in left of [679687]
data in left of [667968]
data in right of [662109]
data in right of [665038]
data in right of [666503]
data in left of [667235]
data in left of [666869]
data in left of [666686]
data in right of [666594]
data in right of [666640]
data in right of [666663]
data in left of [666674]
data in left of [666668]
data in right of [666665]
found 666666
标签:
原文地址:http://www.cnblogs.com/worter991/p/5161253.html