KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I have been working on generating all possible submodels for a biological problem. I have a working recursion for generating a big list of all the submodels I want. However, the lists get unmanageably large pretty fast (N=12 is just possible in the example below, N>12 uses too much memory). So I wanted to convert it to a generator function using yield instead, but I'm stuck. My working recursive function looks like this: def submodel_list(result, pat, current, maxn): ''' result is a list to append to pat is the current pattern (starts as empty list) current is the current number of the pattern maxn is the number of items in the pattern ''' if pat: curmax = max(pat) else: curmax = 0 for i in range(current): if i-1 <= curmax: newpat = pat[:] newpat.append(i) if current == maxn: result.append(newpat) else: submodel_generator(result, newpat, current+1, maxn) result = [] submodel_list(result, [], 1, 5) That gives me the expected list of submodels for my purposes. Now, I want to get that same list using a recursion. Naively, I thought I could just switch out my result.append() for a yield function, and the rest would work OK. So I tried this: def submodel_generator(pat, current, maxn): '''same as submodel_list but yields instead''' if pat: curmax = max(pat) else: curmax = 0 for i in range(current): print i, current, maxn if i-1 <= curmax: print curmax newpat = pat[:] newpat.append(i) if current == maxn: yield newpat else: submodel_generator(newpat, current+1, maxn) b = submodel_generator([], 1, 5) for model in b: print model But now I get nothing. A (very dumb) bit of digging tells me the func
Tags (comma-separated)
Save Edits
Cancel