遍历字典时,我会将键的值分配给另一本字典的匹配键,但是当我这样做时,它将更改字典中所有键的值,而不仅仅是匹配密钥输入。
由于某种原因,这在Sublime中工作正常,但在PyCharm中运行完全相同的代码时则无效。
categories_ranges = {'MILES':
{'CATEGORIES': ['TOTPOP_CY', 'TOTHH_CY', 'MEDHINC_CY',
'AVGHINC_CY', 'BACHDEG_CY', 'GRADDEG_CY', 'MEDAGE_CY',
'FAMHH_CY', 'OCCARCH_CY', 'OCCBLDG_CY', 'OCCFARM_CY',
'OCCCONS_CY', 'OCCMAIN_CY', 'OCCTRAN_CY'],
'RANGES': [[1, 2, 3, 5]]}, 'MINUTES':
{'CATEGORIES': '', 'RANGES': ''}}
organized_output = {
'TOTPOP_CY': {'MILES': {1: 0, 2: 0, 3: 0, 5: 0}, 'MINUTES': {}},
'TOTHH_CY': {'MILES': {1: 0, 2: 0, 3: 0, 5: 0}, 'MINUTES': {}},
'MEDHINC_CY': {'MILES': {1: 0, 2: 0, 3: 0, 5: 0}, 'MINUTES': {}},
'AVGHINC_CY': {'MILES': {1: 0, 2: 0, 3: 0, 5: 0}, 'MINUTES': {}},
'BACHDEG_CY': {'MILES': {1: 0, 2: 0, 3: 0, 5: 0}, 'MINUTES': {}},
'GRADDEG_CY': {'MILES': {1: 0, 2: 0, 3: 0, 5: 0}, 'MINUTES': {}},
'MEDAGE_CY': {'MILES': {1: 0, 2: 0, 3: 0, 5: 0}, 'MINUTES': {}},
'FAMHH_CY': {'MILES': {1: 0, 2: 0, 3: 0, 5: 0}, 'MINUTES': {}},
'OCCARCH_CY': {'MILES': {1: 0, 2: 0, 3: 0, 5: 0}, 'MINUTES': {}},
'OCCBLDG_CY': {'MILES': {1: 0, 2: 0, 3: 0, 5: 0}, 'MINUTES': {}},
'OCCFARM_CY': {'MILES': {1: 0, 2: 0, 3: 0, 5: 0}, 'MINUTES': {}},
'OCCCONS_CY': {'MILES': {1: 0, 2: 0, 3: 0, 5: 0}, 'MINUTES': {}},
'OCCMAIN_CY': {'MILES': {1: 0, 2: 0, 3: 0, 5: 0}, 'MINUTES': {}},
'OCCTRAN_CY': {'MILES': {1: 0, 2: 0, 3: 0, 5: 0}, 'MINUTES': {}}
}
for key, value in categories_ranges.items():
demogs_response = {'MILES':
{'TOTPOP_CY': {1: 6639, 2: 22682, 3: 39254, 5: 86086},
'TOTHH_CY': {1: 2834, 2: 9149, 3: 16026, 5: 37772},
'MEDHINC_CY': {1: 53505, 2: 52391, 3: 55267, 5: 67726},
'AVGHINC_CY': {1: 72158, 2: 74334, 3: 80261, 5: 100634},
'BACHDEG_CY': {1: 895, 2: 3221, 3: 5805, 5: 16240},
'GRADDEG_CY': {1: 414, 2: 1508, 3: 3021, 5: 11027},
'MEDAGE_CY': {1: 35.2, 2: 36.4, 3: 37.8, 5: 43.6},
'FAMHH_CY': {1: 1545, 2: 5319, 3: 9072, 5: 20572},
'OCCARCH_CY': {1: 104, 2: 382, 3: 466, 5: 956},
'OCCBLDG_CY': {1: 169, 2: 905, 3: 1704, 5: 1471},
'OCCFARM_CY': {1: 0, 2: 0, 3: 3, 5: 21},
'OCCCONS_CY': {1: 152, 2: 791, 3: 1264, 5: 1719},
'OCCMAIN_CY': {1: 177, 2: 365, 3: 507, 5: 837},
'OCCTRAN_CY': {1: 200, 2: 810, 3: 1537, 5: 1955}}}
if categories_ranges[key]['RANGES'] != '':
for demog_field in demogs_response:
for category in demogs_response[demog_field]:
for _key, _value in demogs_response[demog_field][category].items():
organized_output[category][demog_field][_key] = _value
print("organized_output: ", organized_output)
输出应如下所示-再次可以在Sublime中正常工作
organized_output: {'TOTPOP_CY':
{'MILES': {1: 6639, 2: 22682, 3: 39254, 5: 86086}, 'MINUTES': {}},
'TOTHH_CY': {'MILES': {1: 2834, 2: 9149, 3: 16026, 5: 37772}, 'MINUTES': {}},
'MEDHINC_CY': {'MILES': {1: 53505, 2: 52391, 3: 55267, 5: 67726}, 'MINUTES': {}},
'AVGHINC_CY': {'MILES': {1: 72158, 2: 74334, 3: 80261, 5: 100634}, 'MINUTES': {}},
'BACHDEG_CY': {'MILES': {1: 895, 2: 3221, 3: 5805, 5: 16240}, 'MINUTES': {}},
'GRADDEG_CY': {'MILES': {1: 414, 2: 1508, 3: 3021, 5: 11027}, 'MINUTES': {}},
'MEDAGE_CY': {'MILES': {1: 35.2, 2: 36.4, 3: 37.8, 5: 43.6}, 'MINUTES': {}},
'FAMHH_CY': {'MILES': {1: 1545, 2: 5319, 3: 9072, 5: 20572}, 'MINUTES': {}},
'OCCARCH_CY': {'MILES': {1: 104, 2: 382, 3: 466, 5: 956}, 'MINUTES': {}},
'OCCBLDG_CY': {'MILES': {1: 169, 2: 905, 3: 1704, 5: 1471}, 'MINUTES': {}},
'OCCFARM_CY': {'MILES': {1: 0, 2: 0, 3: 3, 5: 21}, 'MINUTES': {}},
'OCCCONS_CY': {'MILES': {1: 152, 2: 791, 3: 1264, 5: 1719}, 'MINUTES': {}},
'OCCMAIN_CY': {'MILES': {1: 177, 2: 365, 3: 507, 5: 837}, 'MINUTES': {}},
'OCCTRAN_CY': {'MILES': {1: 200, 2: 810, 3: 1537, 5: 1955}, 'MINUTES': {}}}
当前,在PyCharm中,输出如下所示:
organized_output = {'TOTPOP_CY': {'MILES': {1: 200, 2: 810, 3: 1537, 5: 1955}, 'MINUTES': {}}, 'TOTHH_CY': {'MILES': {1: 200, 2: 810, 3: 1537, 5: 1955}, 'MINUTES': {}}, 'MEDHINC_CY': {'MILES': {1: 200, 2: 810, 3: 1537, 5: 1955}, 'MINUTES': {}}, 'AVGHINC_CY': {'MILES': {1: 200, 2: 810, 3: 1537, 5: 1955}, 'MINUTES': {}}, 'BACHDEG_CY': {'MILES': {1: 200, 2: 810, 3: 1537, 5: 1955}, 'MINUTES': {}}, 'GRADDEG_CY': {'MILES': {1: 200, 2: 810, 3: 1537, 5: 1955}, 'MINUTES': {}}, 'MEDAGE_CY': {'MILES': {1: 200, 2: 810, 3: 1537, 5: 1955}, 'MINUTES': {}}, 'FAMHH_CY': {'MILES': {1: 200, 2: 810, 3: 1537, 5: 1955}, 'MINUTES': {}}, 'OCCARCH_CY': {'MILES': {1: 200, 2: 810, 3: 1537, 5: 1955}, 'MINUTES': {}}, 'OCCBLDG_CY': {'MILES': {1: 200, 2: 810, 3: 1537, 5: 1955}, 'MINUTES': {}}, 'OCCFARM_CY': {'MILES': {1: 200, 2: 810, 3: 1537, 5: 1955}, 'MINUTES': {}}, 'OCCCONS_CY': {'MILES': {1: 200, 2: 810, 3: 1537, 5: 1955}, 'MINUTES': {}}, 'OCCMAIN_CY': {'MILES': {1: 200, 2: 810, 3: 1537, 5: 1955}, 'MINUTES': {}}, 'OCCTRAN_CY': {'MILES': {1: 200, 2: 810, 3: 1537, 5: 1955}, 'MINUTES': {}}}
当我在PyCharm的调试中运行此代码时,我可以看到每个“ MILES”字典同时更改其值,这就是为什么“ MILES”字典的每个实例都具有相同的精确值的原因。每个条目都是不同条目的一部分,因此不应该发生这种情况。
我尝试换掉items()
的用法,改为使用zip
,但这也不起作用。我尝试将值赋值过程编写为列表理解,但结果也相同。目前,我非常确信这是PyCharm的问题。我上面提到的每种替代方法在Sublime中都可以完美地工作,但每次在PyCharm中我都得到相同的结果。目前,我正在运行Python 3.6.5,以防万一。
我是不是在这里丢失了某些东西,还是PyCharm存在一些固有的问题,这些问题在这些词典的迭代过程中被打破了?
答案 0 :(得分:0)
这是我确定的最佳解决方案:
output = {}
if categories_ranges[range_type]['RANGES'] != '':
for demog_field in demogs_response:
for category in demogs_response[demog_field]:
output[category] = {}
output[category][demog_field] = {}
for _key, _value in demogs_response[demog_field][category].items():
output[category][demog_field][_key] = _value
return output
else:
return output
这不是尝试为初始词典中的键分配新值,而是形成一个新词典并在遍历先前词典的过程中对其进行重建。