即使在保留副本之后,Python嵌套字典也会被覆盖

时间:2018-05-20 16:55:05

标签: python python-3.x dictionary

我有一个嵌套字典来查找一些信息。由于python标记数据的方式,我使用dict的copy方法创建了dicts的副本。但是当我更新最里面的字典时,它会改变所有其他日子的其他字符(星期一到星期五)。

以下是我如何宣传我的主词典: self.faculty_to_day_hour_slot_map

self.faculty_to_day_hour_slot_map = {}
day_to_hour = {}
faculty_to_data = {} 

hour_to_allotflag = {}
for hour in range(1 , 9):
    hour_to_allotflag.update({hour : {'alloted': False}.copy()}.copy()) ;

for day in ['mon' , 'tue' , 'wed' , 'thu' , 'fri' , 'sat']:
    day_to_hour.update({day : hour_to_allotflag.copy()}.copy()) ;

for faculty_object in self.faculties:
    self.faculty_to_day_hour_slot_map.update({faculty_object.id : day_to_hour.copy()}.copy()) ;

当我像这样更新最里面的dict时会出现问题:

self.faculty_to_day_hour_slot_map.get(faculty).get(day).get(hour)['alloted'] = True

例如:

self.faculty_to_day_hour_slot_map.get('uniqueid').get('mon').get(1)['alloted'] = True 

这使得所有'分配'在每天(星期一到星期五)内为真,小时= 1

如何解决这个问题?尽管使用copy方法,为什么会出现这种情况?

原始字典如下所示:

{51: {'mon': {1: {'alloted': True},
   2: {'alloted': True},
   3: {'alloted': True},
   4: {'alloted': True},
   5: {'alloted': True},
   6: {'alloted': True},
   7: {'alloted': True},
   8: {'alloted': True}},
  'tue': {1: {'alloted': True},
   2: {'alloted': True},
   3: {'alloted': True},
   4: {'alloted': True},
   5: {'alloted': True},
   6: {'alloted': True},
   7: {'alloted': True},
   8: {'alloted': True}},
  'wed': {1: {'alloted': True},
   2: {'alloted': True},
   3: {'alloted': True},
   4: {'alloted': True},
   5: {'alloted': True},
   6: {'alloted': True},
   7: {'alloted': True},
   8: {'alloted': True}},
  'thu': {1: {'alloted': True},
   2: {'alloted': True},
   3: {'alloted': True},
   4: {'alloted': True},
   5: {'alloted': True},
   6: {'alloted': True},
   7: {'alloted': True},
   8: {'alloted': True}},
  'fri': {1: {'alloted': True},
   2: {'alloted': True},
   3: {'alloted': True},
   4: {'alloted': True},
   5: {'alloted': True},
   6: {'alloted': True},
   7: {'alloted': True},
   8: {'alloted': True}},
  'sat': {1: {'alloted': True},
   2: {'alloted': True},
   3: {'alloted': True},
   4: {'alloted': True},
   5: {'alloted': True},
   6: {'alloted': True},
   7: {'alloted': True},
   8: {'alloted': True}}},
 15: {'mon': {1: {'alloted': True},
   2: {'alloted': True},
   3: {'alloted': True},
   4: {'alloted': True},
   5: {'alloted': True},
   6: {'alloted': True},
   7: {'alloted': True},
   8: {'alloted': True}},
  'tue': {1: {'alloted': True},
   2: {'alloted': True},
   3: {'alloted': True},
   4: {'alloted': True},
   5: {'alloted': True},
   6: {'alloted': True},
   7: {'alloted': True},
   8: {'alloted': True}},
  'wed': {1: {'alloted': True},
   2: {'alloted': True},
   3: {'alloted': True},
   4: {'alloted': True},
   5: {'alloted': True},
   6: {'alloted': True},
   7: {'alloted': True},
   8: {'alloted': True}},
  'thu': {1: {'alloted': True},
   2: {'alloted': True},
   3: {'alloted': True},
   4: {'alloted': True},
   5: {'alloted': True},
   6: {'alloted': True},
   7: {'alloted': True},
   8: {'alloted': True}},
  'fri': {1: {'alloted': True},
   2: {'alloted': True},
   3: {'alloted': True},
   4: {'alloted': True},
   5: {'alloted': True},
   6: {'alloted': True},
   7: {'alloted': True},
   8: {'alloted': True}},
  'sat': {1: {'alloted': True},
   2: {'alloted': True},
   3: {'alloted': True},
   4: {'alloted': True},
   5: {'alloted': True},
   6: {'alloted': True},
   7: {'alloted': True},
   8: {'alloted': True}}},
 16: {'mon': {1: {'alloted': True},
   2: {'alloted': True},
   3: {'alloted': True},
   4: {'alloted': True},
   5: {'alloted': True},
   6: {'alloted': True},
   7: {'alloted': True},
   8: {'alloted': True}},
  'tue': {1: {'alloted': True},
   2: {'alloted': True},
   3: {'alloted': True},
   4: {'alloted': True},
   5: {'alloted': True},
   6: {'alloted': True},
   7: {'alloted': True},
   8: {'alloted': True}},
  'wed': {1: {'alloted': True},
   2: {'alloted': True},
   3: {'alloted': True},
   4: {'alloted': True},
   5: {'alloted': True},
   6: {'alloted': True},
   7: {'alloted': True},
   8: {'alloted': True}},
  'thu': {1: {'alloted': True},
   2: {'alloted': True},
   3: {'alloted': True},
   4: {'alloted': True},
   5: {'alloted': True},
   6: {'alloted': True},
   7: {'alloted': True},
   8: {'alloted': True}},
  'fri': {1: {'alloted': True},
   2: {'alloted': True},
   3: {'alloted': True},
   4: {'alloted': True},
   5: {'alloted': True},
   6: {'alloted': True},
   7: {'alloted': True},
   8: {'alloted': True}},
  'sat': {1: {'alloted': True},
   2: {'alloted': True},
   3: {'alloted': True},
   4: {'alloted': True},
   5: {'alloted': True},
   6: {'alloted': True},
   7: {'alloted': True},
   8: {'alloted': True}}},
 17: {'mon': {1: {'alloted': True},
   2: {'alloted': True},
   3: {'alloted': True},
   4: {'alloted': True},
   5: {'alloted': True},
   6: {'alloted': True},
   7: {'alloted': True},
   8: {'alloted': True}},
  'tue': {1: {'alloted': True},
   2: {'alloted': True},
   3: {'alloted': True},
   4: {'alloted': True},
   5: {'alloted': True},
   6: {'alloted': True},
   7: {'alloted': True},
   8: {'alloted': True}},
  'wed': {1: {'alloted': True},
   2: {'alloted': True},
   3: {'alloted': True},
   4: {'alloted': True},
   5: {'alloted': True},
   6: {'alloted': True},
   7: {'alloted': True},
   8: {'alloted': True}},
  'thu': {1: {'alloted': True},
   2: {'alloted': True},
   3: {'alloted': True},
   4: {'alloted': True},
   5: {'alloted': True},
   6: {'alloted': True},
   7: {'alloted': True},
   8: {'alloted': True}},
  'fri': {1: {'alloted': True},
   2: {'alloted': True},
   3: {'alloted': True},
   4: {'alloted': True},
   5: {'alloted': True},
   6: {'alloted': True},
   7: {'alloted': True},
   8: {'alloted': True}},
  'sat': {1: {'alloted': True},
   2: {'alloted': True},
   3: {'alloted': True},
   4: {'alloted': True},
   5: {'alloted': True},
   6: {'alloted': True},
   7: {'alloted': True},
   8: {'alloted': True}}},
 18: {'mon': {1: {'alloted': True},
   2: {'alloted': True},
   3: {'alloted': True},
   4: {'alloted': True},
   5: {'alloted': True},
   6: {'alloted': True},
   7: {'alloted': True},
   8: {'alloted': True}},
  'tue': {1: {'alloted': True},
   2: {'alloted': True},
   3: {'alloted': True},
   4: {'alloted': True},
   5: {'alloted': True},
   6: {'alloted': True},
   7: {'alloted': True},
   8: {'alloted': True}},
  'wed': {1: {'alloted': True},
   2: {'alloted': True},
   3: {'alloted': True},
   4: {'alloted': True},
   5: {'alloted': True},
   6: {'alloted': True},
   7: {'alloted': True},
   8: {'alloted': True}},
  'thu': {1: {'alloted': True},
   2: {'alloted': True},
   3: {'alloted': True},
   4: {'alloted': True},
   5: {'alloted': True},
   6: {'alloted': True},
   7: {'alloted': True},
   8: {'alloted': True}},
  'fri': {1: {'alloted': True},
   2: {'alloted': True},
   3: {'alloted': True},
   4: {'alloted': True},
   5: {'alloted': True},
   6: {'alloted': True},
   7: {'alloted': True},
   8: {'alloted': True}},
  'sat': {1: {'alloted': True},
   2: {'alloted': True},
   3: {'alloted': True},
   4: {'alloted': True},
   5: {'alloted': True},
   6: {'alloted': True},
   7: {'alloted': True},
   8: {'alloted': True}}},
 19: {'mon': {1: {'alloted': True},
   2: {'alloted': True},
   3: {'alloted': True},
   4: {'alloted': True},
   5: {'alloted': True},
   6: {'alloted': True},
   7: {'alloted': True},
   8: {'alloted': True}},
  'tue': {1: {'alloted': True},
   2: {'alloted': True},
   3: {'alloted': True},
   4: {'alloted': True},
   5: {'alloted': True},
   6: {'alloted': True},
   7: {'alloted': True},
   8: {'alloted': True}},
  'wed': {1: {'alloted': True},
   2: {'alloted': True},
   3: {'alloted': True},
   4: {'alloted': True},
   5: {'alloted': True},
   6: {'alloted': True},
   7: {'alloted': True},
   8: {'alloted': True}},
  'thu': {1: {'alloted': True},
   2: {'alloted': True},
   3: {'alloted': True},
   4: {'alloted': True},
   5: {'alloted': True},
   6: {'alloted': True},
   7: {'alloted': True},
   8: {'alloted': True}},
  'fri': {1: {'alloted': True},
   2: {'alloted': True},
   3: {'alloted': True},
   4: {'alloted': True},
   5: {'alloted': True},
   6: {'alloted': True},
   7: {'alloted': True},
   8: {'alloted': True}},
  'sat': {1: {'alloted': True},
..... and so on 

2 个答案:

答案 0 :(得分:1)

copy()函数创建对象的浅表副本。你可能需要的是一份深刻的副本。

import copy
d1 = {"Strongest Avenger": "Thor"}
d2 = copy.deepcopy(d1)

https://docs.python.org/2/library/copy.html

答案 1 :(得分:1)

正如评论和上一个答案所指出的,你应该使用deepcopy。另外,为了便于阅读,您可以使用functools.reduce创建嵌套的dict

from functools import reduce
from copy import deepcopy

faculty = list(range(10))
days = ['mon' , 'tue' , 'wed' , 'thu' , 'fri' , 'sat']
hours = list(range(1, 9))

res = reduce(
        lambda d, ks: {k: deepcopy(d) for k in ks},
        [hours, days, faculty],
        {'alloted': True}
        )