标签:python
#!/usr/bin/python # coding:utf-8 #共有9道题 #http://pythontutor.com/visualize.html#mode=display 调试工具 #1.为什么要用yield而不是print ,yield 循环,有print就会循环输出几次 #执行流程,外层for循环,然后自己循环,又一次for循环 #而且是先输出print,然后是yield ‘‘‘ def item_iterator(embed_list): for item in embed_list: if isinstance(item, (tuple, list)): # print(type(item_iterator(item))) for item in item_iterator(item): yield item else: yield item lst = (0, (1, 2), (3, (4, 5))) for item in item_iterator(lst): print item ‘‘‘ #2.next放主函数,调用next()表达式的值时,其恢复点yield的值总是为None ‘‘‘ import time def A(): while True: print("----我是A函数---") # yield yield 5 time.sleep(0.5) print("AAAAAAAAAAAA") def B(c): while True: print("----我是B函数---") next(c) #next会不断从yield处暂停和恢复 time.sleep(0.5) print("BBBBBBBBBBBB") if __name__ == ‘__main__‘: a = A() print "中断" B(a) ‘‘‘ #3.next 恢复点为None,如果两次next,那么返回值为None #send放入主函数,send(value)方法是将值传给恢复点yield ‘‘‘ def framework(logic): try: it = logic() s = next(it) print "[FX] logic: ", s print "[FX] do something" it.send("async:" + s) except StopIteration: pass def logic(): s = "mylogic2" r = yield s print r framework(logic) ‘‘‘ #4.next 这个因为while条件,会向下走 ‘‘‘ def fibonacci(): a,b=0,1 while True: yield b a, b = b, a + b fib=fibonacci() print fib.next() print fib.next() print fib.next() print [fib.next() for i in range(10)] ‘‘‘ #迭代器必须要有next,才能输出 ‘‘‘ def consumer(name): print("%s 准备吃包子啦!" %name) while True: baozi = yield print("包子[%s]来了,被[%s]吃了!" %(baozi,name)) c = consumer("alex") c.next() c.send`("韭菜馅") ‘‘‘ #yield + 循环 只要函数中有yield,循环的时候,那么就是迭代器,不要使用print ‘‘‘ def item_iterator(embed_list): for item in embed_list: if isinstance(item, (tuple, list)): for i in item_iterator(item) yield i else: yield item lst = (0, (1, 2), (3, (4, 5))) for item in item_iterator(lst): print item ‘‘‘ #素数 质数定义为在大于1的自然数中,除了1和它本身以外不再有其他因数 ‘‘‘ def prime_sieve(n): flags = [True] * n flags[0] = flags[1] = False for i in xrange(2, n): if flags[i]: yield i for j in xrange(2, (n - 1) / i + 1): flags[i * j] = False for p in prime_sieve(100): print p ‘‘‘ ‘‘‘ def prime_sieve(n): flags = {} for i in xrange(2, n): for j in xrange(2, (n - 1) / i + 1): if i==j and i / j == 0: # print i,j flags[i] = False print flags for key in flags.keys(): if flags[key]: yield key for i in prime_sieve(100): print i ‘‘‘ #yield + 循环 对一个序列进行全排列,这个例子说明yield 可以yield ‘‘‘ def permutations(li): if len(li) == 0: yield li else: for i in range(len(li)): li[0], li[i] = li[i], li[0] for item in permutations(li[1:]): # 有for循环,就会输出yield内容 # [li[0]] + item+permutations(li[1:]) yield [li[0]] + item for item in permutations(range(3)): print item ‘‘‘ #八皇后 http://blog.csdn.net/u014386870/article/details/44098151 from itertools import * cols = range(8) for vec in permutations(cols): if (8 == len(set(vec[i]+i for i in cols)) == len(set(vec[i]-i for i in cols)) == len(set(vec[i]+1 for i in cols)) == len(set(vec[i]-1 for i in cols))): print vec
本文出自 “要有梦想,万一实现了呢” 博客,请务必保留此出处http://szgb17.blog.51cto.com/340201/1983825
标签:python
原文地址:http://szgb17.blog.51cto.com/340201/1983825