【问题标题】:Why are my dictionary values changing after the loop?为什么我的字典值在循环后会发生变化?
【发布时间】:2015-03-29 23:49:15
【问题描述】:

以下是我的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}}}

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

【问题讨论】:

标签: python dictionary


【解决方案1】:

当您的程序将值添加到 sumOperator 字典时会出现问题,主要是循环中的最后一个 else 语句。您正在尝试从另一个字典的值构建此字典,因此您有 sumOperator[key] = value,其中 value 指的是另一个字典中的项目。这只是将一个字典指向另一个字典的值;基础价值不会被复制。因此,当您更新 sumOperator 的值时,主 data 字典也会更新。要解决此问题,只需在将值添加到新字典时复制它们即可。

以下代码有效,我们只需在更新时添加copy() sumOperator

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 文档页面。如果您不想修改原件,请务必记住何时需要创建显式副本。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多