python dictionary weird behavior

linuxoid
  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? Why does dict1['A'] change from 0? I don't change any of the dict1 variables!
  2. Why does dict3 show correct values within the loop, but only shows values from the last iteration 3 after the loop is finished.

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 g.d.d.c, that the copy() is what I wanted:

        dict2 = dict1.copy()
g.d.d.c

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. You can do a couple of things to work around this:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

python dictionary weird behavior with dict as container

From Dev

Weird Python behavior while using Dictionary and escape characters

From Dev

Python datetime weird behavior

From Dev

python thread weird behavior

From Dev

Set weird behavior (python)

From Dev

Weird scoping behavior in python

From Dev

Weird performance behavior of .NET dictionary insertion

From Dev

Weird behavior in Python in array initialization

From Dev

Python, weird memory consumption behavior

From Dev

Weird behavior in python list concatenation

From Dev

The weird behavior of the print function in python

From Dev

Weird python file path behavior

From Dev

Weird sets behavior in python REPL

From Dev

Weird behavior with a lot of numbers python

From Python

Unanticipated Python Dictionary Behavior

From Dev

Strange behavior with python dictionary

From Dev

Please explain this behavior of Python dictionary

From Dev

Weird Behavior When Slicing a List in Python

From Dev

Python Flask weird logging behavior (kubernetes)

From Dev

Python: Weird behavior while using `yield from`

From Dev

Knapsack algorithm, weird behavior (python3)

From Dev

Python getting ranks of elements in list weird behavior

From Dev

Weird behavior of barplot from python matplotlib with datetime

From Dev

multiprocessing.Queue weird python behavior

From Dev

Decimal Python Library has weird behavior

From Dev

Weird behavior of (^)

From Dev

Python: Transform weird dictionary into pandas - dataframe

From Dev

Python behavior in replacing dictionary keys under iteration

From Dev

python dictionary iteritems() and keys() synchronized behavior