【发布时间】:2014-01-18 13:17:00
【问题描述】:
我正在使用递归来获取列表的排列。这是我写的,但yield 版本不起作用:
def test_permutation_rec():
print "test 2"
permutation_rec2([1,2,3],[])
print "test 1"
for one in permutation_rec1([1,2,3],[]):
print "one:",one
def permutation_rec1(onelist,prelist):
if onelist == [] :
print prelist
yield prelist
lenlist= len(onelist)
for i, oneitem in enumerate(onelist) :
leftlist = [onelist[j] for j in range(0,lenlist) if j != i]
permutation_rec1(leftlist,prelist + [oneitem])
def permutation_rec2(onelist,prelist):
if onelist == [] :
print prelist
lenlist= len(onelist)
for i, oneitem in enumerate(onelist) :
leftlist = [onelist[j] for j in range(0,lenlist) if j != i]
permutation_rec2(leftlist,prelist + [oneitem])
if __name__ == "__main__":
test_permutation_rec()
结果:
test 2
[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]
test 1
我想我使用 yield 就像在 this the answer 中一样。
谁能告诉我为什么yield不生效?
顺便说一句,leftlist = [onelist[j] for j in range(0,lenlist) if j != i]
在permutation_rec2,我觉得很丑。当列表很大时,它会创建许多临时列表。我该如何改进呢?
【问题讨论】:
-
供将来参考 - 如果您确实需要获得排列,只需使用
itertools模块。 -
是的。你是对的。像
itertools.permutations([1,2,3])这样的东西有人能找到我发布的 tmplist 吗?看来这不是最好的解决方案。 -
我遗漏了什么可以添加以使您不接受我的回答吗? :-)
-
我昨天查看了答案。对不起,可能是点击错误,
标签: python algorithm recursion generator yield