Python:字典链接数据

Spe*_*pre 2 python dictionary

d1 = {"dog":"woof", "cat":"meow"}
d2 = d1
d2["dog"] = "bark"
for i in d1:
    print(i, d1[i])

dog bark
cat meow
Run Code Online (Sandbox Code Playgroud)

这样做的最佳方式是什么:

dog woof
cat meow
Run Code Online (Sandbox Code Playgroud)

jtb*_*des 6

d1 = {"dog":"woof", "cat":"meow"}
d2 = d1.copy() # make a copy, not a reference to the same dictionary
d2["dog"] = "bark"
for i in d1:
    print(i, d1[i])

# dog woof
# cat meow
Run Code Online (Sandbox Code Playgroud)