循环后为什么我的字典值会发生变化?

时间:2015-03-29 13:50:30

标签: python dictionary

以下是我的python代码:

from collections import defaultdict
data = {"Manoj":{"India":{"Airtel":2000,"Vodafone":5000},"Pakistan":{"Airtel":3000}},
    "Shafiq":{"Pakistan":{"Airtel":5000,"BP":10000}},
    "Shams":{"India":{"BP":400}},
    "Govind":{"India":{"Airtel":3000,"Vodafone":2000}},
    "Zakir":{"SriLanka":{"Etisalat":7000}}
    }

print("Printing data:")
print(data)
sumOperator={}
countryCustomers = defaultdict(list)

sum=0

for name,value in data.items():
    for country,value2 in value.items():
        countryCustomers[country].append(name)
        if country not in sumOperator:
            sumOperator[country]=value2
        else:
            for operator,value3 in value2.items():
                if operator not in sumOperator[country]:
                    sumOperator[country][operator] = value3
                else:
                    sum = sumOperator[country][operator] + value3
                    sumOperator[country][operator] = sum

print("")
print("Printing sumOperator:")
print(sumOperator)
print("")
print("Priniting data:")
print(data)

Output:
Printing data:
{'Zakir': {'SriLanka': {'Etisalat': 7000}}, 'Manoj': {'Pakistan': {'Airtel':3000}, 'India': {'Vodafone': 5000, 'Airtel': 2000}}, 'Shams': {'India': {'BP': 400}}, 'Shafiq': {'Pakistan': {'BP': 10000, 'Airtel': 5000}}, 'Govind': {'India': {'Vodafone': 2000, 'Airtel': 3000}}}

Printing sumOperator:
{'Pakistan': {'BP': 10000, 'Airtel': 8000}, 'SriLanka': {'Etisalat': 7000}, 'India': {'Vodafone': 7000, 'BP': 400, 'Airtel': 5000}}

Priniting data:
{'Zakir': {'SriLanka': {'Etisalat': 7000}}, 'Manoj': {'Pakistan': {'BP': 10000, 'Airtel': 8000}, 'India': {'Vodafone': 7000, 'BP': 400, 'Airtel': 5000}}, 'Shams': {'India': {'BP': 400}}, 'Shafiq': {'Pakistan': {'BP': 10000, 'Airtel': 5000}}, 'Govind': {'India': {'Vodafone': 2000, 'Airtel': 3000}}}

为什么我的字典“数据”在初始化另一个字典后会改变值?最后一个打印数据语句生成另一个数据字典,它与开头

中初始化的数据字典不同

1 个答案:

答案 0 :(得分:2)

当您的程序将值添加到 sumOperator 字典时,并且主要使用循环中的最终else语句时,会出现问题。您正尝试根据另一个字典的值构建此字典,因此您有sumOperator[key] = value,其中value指的是另一个字典中的项目。这只是将一个字典指向另一个字典的值;不复制基础值。因此,当您更新 sumOperator 的值时,主数据字典也会更新。要解决此问题,只需在将值添加到新词典时复制值。

以下代码有效,只要我们更新 sumOperator ,我们就会添加copy()

from collections import defaultdict
from copy import copy
data = {"Manoj":{"India":{"Airtel":2000,"Vodafone":5000},"Pakistan":{"Airtel":3000}},
        "Shafiq":{"Pakistan":{"Airtel":5000,"BP":10000}},
        "Shams":{"India":{"BP":400}},
        "Govind":{"India":{"Airtel":3000,"Vodafone":2000}},
        "Zakir":{"SriLanka":{"Etisalat":7000}}
        }

print("Printing data:")
print(data)

sumOperator={}
countryCustomers = defaultdict(list)

sum=0

for name,value in data.items():
    for country,value2 in value.items():
        countryCustomers[country].append(name)
        if country not in sumOperator:
            sumOperator[country] = copy(value2)
        else:
            for operator,value3 in value2.items():
                if operator not in sumOperator[country]:
                    sumOperator[country][operator] = copy(value3)
                else:
                    sum = sumOperator[country][operator] + copy(value3)
                    sumOperator[country][operator] = sum


print("Printing sumOperator:")
print(sumOperator)

print("Priniting data:")
print(data)

正如其他人所指出的那样,您可能希望在assignment statements and copying上查看Python文档页面。如果您不想修改原件,请务必记住何时需要创建显式副本。