我尝试更改
newperm = perm
newperm.insert(i,elements[0])
yield newperm
这行到
perm.insert(i,elements[0])
yield perm
perm.pop()
它起作用了,但是我不明白为什么以前的那个不起作用
def all_perms(elements):
if len(elements) <=1:
yield elements
else:
for perm in all_perms(elements[1:]):
for i in range(len(elements)):
newperm = perm
newperm.insert(i,elements[0])
yield newperm
for i in all_perms([1,2,3]):
print(i)