简体   繁体   English

Python字典奇怪的行为

[英]python dictionary weird behavior

  1. I can't understand why, when c=2 and c=3, the dict2['A']=1 and dict2['A']=4 respectively and not 0, even though I make dict2 to be equal to dict1, where dict1['A']=0? 我不明白为什么,当我将dict2等于dict1时,当c = 2和c = 3时,dict2 ['A'] = 1和dict2 ['A'] = 4而不是0, dict1 ['A'] = 0在哪里? Why does dict1['A'] change from 0? 为什么dict1 ['A']从0变为? I don't change any of the dict1 variables! 我不更改任何dict1变量!
  2. Why does dict3 show correct values within the loop, but only shows values from the last iteration 3 after the loop is finished. 为什么dict3在循环中显示正确的值,而只显示循环完成后最后一次迭代3中的值。

Thank you very much. 非常感谢你。

import collections

def main():


    dict1 = collections.OrderedDict()
    dict2 = collections.OrderedDict()
    dict3 = collections.OrderedDict()

    dict1['A'] = 0
    dict1['B'] = 0
    dict1['C'] = 0

    for c in [1, 2, 3]:
        print('c=' + str(c))
        dict2 = dict1
        print('dict1A=' + str(dict1['A']))
        print('dict2A=' + str(dict2['A']))
        if c == 1:
            dict2['A'] = 1
            dict2['B'] = 2
            dict2['C'] = 3
        elif c ==2:
            dict2['A'] = 4
            dict2['B'] = 5
            dict2['C'] = 6
        elif c ==3:
            dict2['A'] = 7
            dict2['B'] = 8
            dict2['C'] = 9
        dict3['c' + str(c)] = dict2
        print('dict2A=' + str(dict2['A']))
        print('dict' + str(c) + 'A=' + str(dict3['c' + str(c)]['A']))
        print('dict' + str(c) + 'B=' + str(dict3['c' + str(c)]['B']))
        print('dict' + str(c) + 'C=' + str(dict3['c' + str(c)]['C']))

    print('dict3-c1A='+ str(dict3['c1']['A']))
    print('dict3-c2B=' + str(dict3['c2']['B']))
    print('dict3-c3C=' + str(dict3['c3']['C']))

if __name__ == '__main__':
    main()

Output: 输出:

c=1
dict1A=0
dict2A=0
dict2A=1
dict1A=1
dict1B=2
dict1C=3
c=2
dict1A=1
dict2A=1
dict2A=4
dict2A=4
dict2B=5
dict2C=6
c=3
dict1A=4
dict2A=4
dict2A=7
dict3A=7
dict3B=8
dict3C=9
dict3-c1A=7
dict3-c2B=8
dict3-c3C=9

* EDIT * Thank you very much for the answers. *编辑*非常感谢您的回答。 I didn't know the '=' operation for dictionaries was not the same as for variables. 我不知道字典的'='操作与变量的不同。 I found out and as was suggested by gddc, that the copy() is what I wanted: 我发现并按照gddc的建议,copy()是我想要的:

        dict2 = dict1.copy()
  1. This is how the Python object model works. 这就是Python对象模型的工作方式。 by saying dict2 = dict1 , what in essence happens during the first loop iteration is that the original dict2 object is discarded, and dict2 now refers to the same underlying object as dict 1 . 通过说dict2 = dict1 ,本质上在第一次循环迭代期间发生的事情是原始dict2对象被丢弃,而dict2现在引用与dict 1相同的基础对象 During subsequent loop iterations, the dict2 = dict1 statement has no effect since dict1 and dict2 are already both pointing at the same underlying object. 在随后的循环迭代中, dict2 = dict1语句无效,因为dict1dict2已经都指向相同的基础对象。

  2. Your print statements for dict3 are one level of indentation further out than your loop body; 您对dict3的打印语句比循环主体更深一层。 they are not part of the loop, and therefore only execute once the loop is finished. 它们不是循环的一部分,因此仅在循环完成后才执行。

dict2 = dict1

This doesn't erase the contents of dict2 and fill it with the contents of dict1 . 这不删除的内容dict2与内容填充它dict1 This makes the names dict1 and dict2 refer to the same object. 这使得名称dict1dict2指向同一对象。 Anything you do to dict2 affects dict1 , because they're the same thing. 您对dict2任何事情dict2影响dict1 ,因为它们是同一件事。

If you want a new dict, make a new dict: 如果您想要一个新的字典,请做一个新的字典:

dict2 = collections.OrderedDict()

If you want to clear the old dict, clear it, but that's probably not what you want: 如果要清除旧字典,请清除它,但这可能不是您想要的:

dict2.clear()

When you assign dict2 = dict1 you replace the name which previously existed as an empty OrderedDict and tell the interpreter instead to use dict2 to refer to the same Object that exists in the name dict1 . 当您分配dict2 = dict1 ,替换以前作为空OrderedDict存在的名称,并告诉解释器改用dict2引用名称dict1中存在的同一Object You can do a couple of things to work around this: 您可以做一些事情来解决此问题:

# copy
dict2 = dict1.copy()
# update dict 2
dict2.update(dict1.iteritems())

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

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