python - Simple recursion with yield? -
if put this, see combinations in all_items. however, if not want store combinations , put yield instead, not work. idea?
all_items = [] def get_val(n,last): if n < 5: k in range(-1,1+1,1): get_val(n+1,last+[k]) else: get_val(0,[]) all_items += [last]
if understand correctly, should work:
def get_val(n, last): if n < 5: k in range(-1, 1+1, 1): yield get_val(n+1, last+[k]) else: yield last get_val(0, [])
works in python 3.3+, below can replace yield get_val(n+1, last+[k])
with:
for thing in get_val(n+1, last+[k]): yield thing
Comments
Post a Comment