生成器面试题 遇到for循环先列出来

def add(n,i):
    return n+i

def test():
    for i in range(4):
        yield i

g=test()
for n in [1,10,3]:
    g=(add(n,i) for i in g)
'''
n=1
g=(add(n,i) for i in g)
n=2
g=(add(n,i) for i in g)
n=3
g=(add(n,i) for i in g)
n=3
#g=(add(n,i) for i in add(n,i) for i in add(n,i) for i in test())  n=0 1 2 3
            9 10 11 12              6 7 8 9                3 4 5 6
                                                                                                      
'''

print(list(g))

 

posted @ 2018-11-07 10:08  张朝华  阅读(256)  评论(0编辑  收藏  举报