Python 尝试使用递归构建列表。全局列表中的值不为';不变
所以我在做一个问题,我使用一个递归的解决方案。为了简单起见,假设问题是我需要找到给定字符串的所有字谜。(问题很复杂,但这足以说明我在做什么) 我正在使用递归来实现这一点Python 尝试使用递归构建列表。全局列表中的值不为';不变,python,list,python-2.7,recursion,scope,Python,List,Python 2.7,Recursion,Scope,所以我在做一个问题,我使用一个递归的解决方案。为了简单起见,假设问题是我需要找到给定字符串的所有字谜。(问题很复杂,但这足以说明我在做什么) 我正在使用递归来实现这一点 all_anagrams = [] def main(): ... ... get_anagrams() # calls a method that appends anagrams to list all_anagrams print all_anagrams 而get_anagrams的方
all_anagrams = []
def main():
...
...
get_anagrams() # calls a method that appends anagrams to list all_anagrams
print all_anagrams
而get_anagrams
的方法是:
def get_anagrams(user_word, anagram, words, inventory):
'''
Finds all anagrams for the given user_word.
'''
if len(inventory) == 0:
all_anagrams.append(anagram)
else:
for choice in words:
if len(choice) <= len(inventory) and choice in inventory:
anagram.append(choice)
inventory.subtract(choice)
get_anagrams(user_word, anagram, words, inventory)
anagram.pop()
inventory.add(choice)
我已经尝试将列表定义为全局
列表,或者将列表作为参数传递,因为被调用方对列表所做的任何更改也应该反映在调用方中。但什么都没用
为什么会这样?如何修复它?实际的问题在于
附加
和弹出
。当你这样做的时候
all_anagrams.append(anagram)
您正在将对字谜
的引用添加到所有字谜
。但是,当递归展开时,您将弹出字谜
中的值。这就是为什么在main
中打印空列表的原因。立即的解决办法是这样的
if len(choice) <= len(inventory) and choice in inventory:
inventory.subtract(choice)
get_anagrams(user_word, anagram + [choice], words, inventory)
inventory.add(choice)
if len(choice)实际问题在于append
和pop
。当你这样做的时候
all_anagrams.append(anagram)
您正在将对字谜
的引用添加到所有字谜
。但是,当递归展开时,您将弹出字谜
中的值。这就是为什么在main
中打印空列表的原因。立即的解决办法是这样的
if len(choice) <= len(inventory) and choice in inventory:
inventory.subtract(choice)
get_anagrams(user_word, anagram + [choice], words, inventory)
inventory.add(choice)
if len(choice)实际问题在于append
和pop
。当你这样做的时候
all_anagrams.append(anagram)
您正在将对字谜
的引用添加到所有字谜
。但是,当递归展开时,您将弹出字谜
中的值。这就是为什么在main
中打印空列表的原因。立即的解决办法是这样的
if len(choice) <= len(inventory) and choice in inventory:
inventory.subtract(choice)
get_anagrams(user_word, anagram + [choice], words, inventory)
inventory.add(choice)
if len(choice)实际问题在于append
和pop
。当你这样做的时候
all_anagrams.append(anagram)
您正在将对字谜
的引用添加到所有字谜
。但是,当递归展开时,您将弹出字谜
中的值。这就是为什么在main
中打印空列表的原因。立即的解决办法是这样的
if len(choice) <= len(inventory) and choice in inventory:
inventory.subtract(choice)
get_anagrams(user_word, anagram + [choice], words, inventory)
inventory.add(choice)
if len(选择)有趣的方法是使用yield
和yield from
而不是附加到全局列表中。请用有趣的方式;这可能也会解决您的问题。有趣的方式(yield from
)在py 2.7Thenyield
循环中不起作用。另外,如果len(inventory)==0
可以是如果inventory
@minitech,你的意思是,如果不是inventory:
?@fourtheye:是,或者翻转块。(这样看起来更好!)有趣的方法是使用yield
和yield from
而不是附加到全局列表中。请用有趣的方式;这可能也会解决您的问题。有趣的方式(yield from
)在py 2.7Thenyield
循环中不起作用。另外,如果len(inventory)==0
可以是如果inventory
@minitech,你的意思是,如果不是inventory:
?@fourtheye:是,或者翻转块。(这样看起来更好!)有趣的方法是使用yield
和yield from
而不是附加到全局列表中。请用有趣的方式;这可能也会解决您的问题。有趣的方式(yield from
)在py 2.7Thenyield
循环中不起作用。另外,如果len(inventory)==0
可以是如果inventory
@minitech,你的意思是,如果不是inventory:
?@fourtheye:是,或者翻转块。(这样看起来更好!)有趣的方法是使用yield
和yield from
而不是附加到全局列表中。请用有趣的方式;这可能也会解决您的问题。有趣的方式(yield from
)在py 2.7Thenyield
循环中不起作用。另外,如果len(inventory)==0
可以是如果inventory
@minitech,你的意思是,如果不是inventory:
?@fourtheye:是,或者翻转块。(那样看起来更好!)+1。另外,将所有字符作为参数传递,不要使用全局变量。并将其返回。@e-satis Thank:)添加了一条注释,将所有字符
用作参数,而不是用作全局变量。+1。另外,将所有字符作为参数传递,不要使用全局变量。并将其返回。@e-satis Thank:)添加了一条注释,将所有字符
用作参数,而不是用作全局变量。+1。另外,将所有字符作为参数传递,不要使用全局变量。并将其返回。@e-satis Thank:)添加了一条注释,将所有字符
用作参数,而不是用作全局变量。+1。另外,将所有字符作为参数传递,不要使用全局变量。并将其返回。@e-satis Thank:)添加了一个注释,将所有字符
用作参数,而不是用作全局变量。