应为在python中dict是可变对象。用dic1赋值给dic3后,并没有创建一个新的对象,dic3和dic1指向同一个对象。通过dic3改变其内容,就如同通过dic1改变对象内容一样。
具体的内容你可以看看深拷贝和浅拷贝
import?copydef?com_weight(dic1,dic2):
dic3?=?copy.deepcopy(dic1)#深拷贝,,,,
for?i?in?dic1.keys():
for?j?in?dic1[i].keys():
dic3[i][j]=dic1[i][j]-dic2[i][j]
print?dic3
if?__name__=='__main__':
dic1?=?{1:?{1:?142264,?2:?142315},?2:?{3:?142111},?3:?{3:?142723}}
dic2?=?{1:?{1:?142263,?2:?142312},?2:?{3:?142110},?3:?{3:?142722}}?
print?'__________________'
print?dic1
print?dic2
print?'*******************'
com_weight(dic1,?dic2)
print?'~~~~~~~~~~~~~~~~~~~~~~~~'
print?dic1
print?dic2