简体   繁体   English

尝试使用递归构建列表。 全局列表中的值不变

[英]Trying to build a list using recursion. Value in global list doesn't change

So I'm doing a problem, and I use a recursive solution. 所以我在做一个问题,并且我使用了递归解决方案。 For simplicity, let's say the problem is that I need to find all anagrams of a given string. 为简单起见,假设问题是我需要查找给定字符串的所有字谜。 (The problem is much complicated, but this is enough to illustrate what I'm doing). (问题很复杂,但这足以说明我在做什么)。

I am using recursion to do this. 我正在使用递归来做到这一点。

all_anagrams = []

def main():
    ...
    ...
    get_anagrams() # calls a method that appends anagrams to list all_anagrams
    print all_anagrams

and the method get_anagrams is: 方法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 is supposed to be a nested list, where each sub-list is composed of some words. all_anagrams应该是一个嵌套列表,其中每个子列表都由一些单词组成。

Now get_anagrams should change the global value of all_anagrams but it doesn't. 现在get_anagrams应该改变的全球价值all_anagrams但事实并非如此。 In main , it prints [[], [], [], [], [], []] , but within the method it prints the correct list. main ,它打印[[], [], [], [], [], []] ,但是在方法内,它打印正确的列表。

Consider this for example: 请考虑以下示例:

>>> a_list = []
>>> def change_list(some_list):
...     some_list.append(1)
...     some_list.append(2)
...     
>>> 
>>> change_list(a_list)
>>> a_list
[1, 2]

I've tried defining the list as a global list, or passing the list as a parameter since any changes to list made by callee should be reflected in the caller too. 我尝试将列表定义为global列表,或者将列表作为参数传递,因为被调用方对列表所做的任何更改也应反映在调用方中。 But nothing worked. 但是没有任何效果。

Why is it so? 为什么会这样呢? How do I fix it? 我如何解决它?

The actual problem is with the append and pop . 实际的问题是appendpop When you do 当你做

all_anagrams.append(anagram)

You are adding the reference to anagram to all_anagrams . 您正在将对anagram的引用添加到all_anagrams But, when the recursion unwinds, you are popping the values from anagram . 但是,当递归展开时,您将从anagram中弹出值。 Thats why empty lists are printed in main . 这就是为什么在main中打印空列表的原因。 The immediate fix would be like this 立即修复将是这样

            if len(choice) <= len(inventory) and choice in inventory:
            inventory.subtract(choice)
            get_anagrams(user_word, anagram + [choice], words, inventory)
            inventory.add(choice)

And as e-satis suggested, please avoid using global variables and pass all_anagrams as one of the parameters. 正如e-satis所建议的,请避免使用全局变量,而将all_anagrams作为参数之一传递。

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

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