我是发电机的新手.当我用yield替换print时,为什么print语句的第一个正确函数不起作用(Python 2.7)
首先是正确的打印功能:
def make_all_pairs(list_):
pivot = list_.pop(0)
for el in list_:
pair = (pivot, el)
print pair
if len(list_) > 1:
make_all_pairs(list_)
make_all_pairs(["a","b","c","d","e"])
('a', 'b')
('a', 'c')
('a', 'd')
('a', 'e')
('b', 'c')
('b', 'd')
('b', 'e')
('c', 'd')
('c', 'e')
('d', 'e')
Run Code Online (Sandbox Code Playgroud)
然后发电机不给出所有组合
def make_all_pairs(list_):
pivot = list_.pop(0)
for el in list_:
pair = (pivot, el)
yield pair
if len(list_) > 1:
make_all_pairs(list_)
x = make_all_pairs(["a","b","c","d","e"])
for el in x:
print el …Run Code Online (Sandbox Code Playgroud)