Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 词典列表的问题_Python - Fatal编程技术网

Python 词典列表的问题

Python 词典列表的问题,python,Python,我有以下代码: n=4 arr=['abcd', 'abce', 'abcdex', 'abcde'] mainarr=[{}] dic={} for i in range(1,n+1): insert(dic,arr[i-1]+' ') #insert is a function that modifies dic print(dic) #to see whats the current dic mainarr.append(dic.copy

我有以下代码:

n=4
arr=['abcd', 'abce', 'abcdex', 'abcde']
mainarr=[{}]
dic={}
for i in range(1,n+1):
    insert(dic,arr[i-1]+' ') #insert is a function that modifies dic
    print(dic)               #to see whats the current dic
    mainarr.append(dic.copy())
所以我得到了mainarr

现在的问题是,不知何故,mainarr的第一个和第二个元素与预期相同,但其余三个条目都是相同的,并且等于应该是mainarr的最后一个元素的值……请帮助我找出错误

输出是

{'a': 'abcd '}
{'a': {'b': {'c': {'d': 'abcd ', 'e': 'abce '}}}}
{'a': {'b': {'c': {'d': {' ': 'abcd ', 'e': 'abcdex '}, 'e': 'abce '}}}}
{'a': {'b': {'c': {'d': {' ': 'abcd ', 'e': {'x': 'abcdex ', ' ': 'abcde 
'}}, 'e': 'abce '}}}}
因此,dic值在每次迭代后都是正确的

但迈纳尔是

[{}, {'a': 'abcd '}, {'a': {'b': {'c': {'d': {' ': 'abcd ', 'e': {'x': 
'abcdex ', ' ': 'abcde '}}, 'e': 'abce '}}}}, {'a': {'b': {'c': {'d': {' ': 
'abcd ', 'e': {'x': 'abcdex ', ' ': 'abcde '}}, 'e': 'abce '}}}}, {'a': 
{'b': {'c': {'d': {' ': 'abcd ', 'e': {'x': 'abcdex ', ' ': 'abcde '}}, 'e': 
'abce '}}}}] #as you can see, first two elements are correct, but last 3 are 
             equal to final value of dic
如果可能有帮助,插入代码为

def insert(dic,string,depth=0):
    if type(dic)==dict:

        if string[depth] in dic.keys():
            dic[string[depth]]=insert(dic[string[depth]],string,depth+1)
            return dic
        else:
            dic[string[depth]]=string
            return dic
    else:


        if dic[depth]==string[depth]:
             return {dic[depth]:insert(dic,string,depth+1)}

        else:
            return {dic[depth]:dic,string[depth]:string}
dic.copy()。您可以使用
copy
模块中的
copy.deepcopy
。但是,还有另一种方法可以将复制移动到insert函数中,以便它始终返回一个副本

您的insert函数会改变字典:

    if string[depth] in dic.keys():
        dic[string[depth]]=insert(dic[string[depth]],string,depth+1)
        return dic
    else:
        dic[string[depth]]=string
        return dic
如果在insert函数中复制一次,则问题消失:

    dic = dic.copy()
    if string[depth] in dic.keys():
        dic[string[depth]]=insert(dic[string[depth]],string,depth+1)
        return dic
    else:
        dic[string[depth]]=string
        return dic
结果是insert函数总是返回一个副本,然后主循环应该如下所示:

for i in range(1,n+1):
    dic = insert(dic, arr[i-1]+' ') # returns a copy that is mutated
    print(dic)               #to see whats the current dic
    mainarr.append(dic)

您能提供插入()的代码吗?添加了插入()的代码