简体   繁体   English

无堆栈 Python - for 循环中的递归?

[英]Stackless Python - Recursion in a for loop?

I'm fairly new to programming and I've been working with Python for a few months now.我对编程很陌生,我已经使用 Python 几个月了。 I'm trying to get a concept to work with Stackless, but just can't figure out how (though I've written other test scripts that work with Stackless).我试图让一个概念与 Stackless 一起工作,但就是不知道如何(尽管我已经编写了其他与 Stackless 一起工作的测试脚本)。

Anywho, as a boiled down example consider the following code that runs through a list and finds all permutations (edit: n-dimensional cartesian products) of it by calling the same function recursively. Anywho,作为一个简单的例子,考虑以下代码,它遍历一个列表并通过递归调用相同的 function 来找到它的所有排列(编辑:n 维笛卡尔积)。

def traverseList(theList,temp,solutions,level=1):
    if level != len(theList):
        for x in theList:
            temp.append(x)
            traverseList(theList,temp,solutions,level+1)
            temp.pop()
    else:
        for x in theList:
            temp.append(x)
            solutions.append(temp[:])
            temp.pop()

myList = ["a",None,2,"gamma",8] #the list doesn't always have just numbers
solutionList = []
tempList = []

traverseList(myList,tempList,solutionList)
print("%s... %s" %(solutionList[0], solutionList[-1]))

which yields:产生:

['a', 'a', 'a', 'a', 'a']... [8, 8, 8, 8, 8]

So far it seems that the only examples I find with Stackless and recursion have the function sending information out at the end of the function after it's all done.到目前为止,我发现的 Stackless 和递归的唯一示例似乎是 function 在完成后在 function 的末尾发送信息。 Never in the middle of a for loop, as would be necessary in the above.永远不要在 for 循环的中间,就像上面所说的那样。

How the heck would I do this?我该怎么做? How would I turn this into a script that would run with tasklets rather than recursive functions?我如何将它变成一个可以使用 tasklet 而不是递归函数运行的脚本? ( This version is the best I can come up with, but it fails no matter how I arrange it. This is one of many tries, I may as well throw spaghetti up against a wall at this point.) 这个版本是我能想到的最好的版本,但无论我怎么安排都失败了。这是许多尝试中的一个,我不妨在这一点上把意大利面扔到墙上。)

Bonus e-cookie for a way to do it without a bounceBack function - I haven't yet been able to find a way to have a single tasklet pass information to itself multiple times without one.奖金 e-cookie 用于在没有反弹的情况下做到这一点 function - 我还没有找到一种方法让单个 tasklet 将信息多次传递给自己而没有一个。

Thanks for your time!谢谢你的时间!

I think you want to research "generators" (ie "yield" python keyword).我想你想研究“发电机”(即“产量” python 关键字)。 Basically a generator lets you pause in the middle of a function call and sort of return the result.基本上,生成器可以让您在 function 调用中间暂停并返回结果。 When the function is "called" again it resumes at the line just after the "yield".当 function 再次“调用”时,它会在“yield”之后的行处恢复。 When you finally "return" you are done.当您最终“返回”时,您就完成了。

Here's some example code for you:下面是一些示例代码:

def myGen(*x):
  for elem in x:
    print "in myGen"
    yield elem

def myFn(*x):
  ret = []
  for elem in x:
    print "in myFn"
    ret.append(x)
  return x


for e in myGen(1,2,3,4,5):
  print e

for e in myFn(1,2,3,4,5):
  print e

The output is below. output 如下。 Notice in the generator case (myGen), the "in myGen" is printed alternating with the print of the list.请注意,在生成器案例 (myGen) 中,“in myGen”与列表的打印交替打印。 But in the myFn of course "in myFn" is printed out first.但是在 myFn 中当然首先打印出“in myFn”。

in myGen
1
in myGen
2
in myGen
3
in myGen
4
in myGen
5
in myFn
in myFn
in myFn
in myFn
in myFn
1
2
3
4
5

If I've understood your question right and since you already have your method in place, plugging this in would work如果我正确理解了您的问题并且由于您已经制定了方法,那么将其插入即可

import stackless as s
channel = s.channel()
s.tasklet(traverseList)(myList,tempList,solutionList)
s.run()
print("%s... %s" %(solutionList[0], solutionList[-1]))

Alternatively you could use *args / **kwargs in the parameters list to the tasklet或者,您可以在 tasklet 的参数列表中使用 *args / **kwargs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM