python 迭代器 第二部分
yield循环
#为什么要用yield而不是print ,yield 循环,有print就会循环输出几次
#执行流程,外层for循环,然后自己循环,又一次for循环
#而且是先输出print,然后是yield
next,send
#next放主函数,调用next()表达式的值时,其恢复点yield的值总是为None
#next会不断从yield处暂停和恢复
#while True
#next(it #it为函数)
#yield 后面加的不断变化的变量
#c.next() = next(c)
#例子
素数
八皇后(全排列+交叉坐标相加相等)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/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 " logic: ", s
print " 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()
'''
#迭代器必须要有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 = * n
flags = flags = False
for i in xrange(2, n):
if flags:
yield i
for j in xrange(2, (n - 1) / i + 1):
flags = 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 = False
print flags
for key in flags.keys():
if flags:
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, li = li, li
for item in permutations(li):
# 有for循环,就会输出yield内容
# ] + item+permutations(li)
yield ] + 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 for i in cols)) == len(set(vec-i for i in cols)) == len(set(vec+1 for i in cols)) == len(set(vec-1 for i in cols))):
print vec
111111
页:
[1]