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

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -