Вложенный словарь Python перезаписывается даже после сохранения копии - PullRequest
0 голосов
/ 20 мая 2018

У меня есть словарь для поиска информации.Из-за способа пометки данных в python я создал копии dicts, используя метод dict copy.Но когда я обновляю самый внутренний диктат, он меняет другие диктанты всех остальных дней (с понедельника по пятницу).

Вот как я инициализирую свой основной словарь: 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()) ;

Проблема возникает, когда я обновляю самый внутренний словарь, как это:

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 

Это делает все «выделенные» как 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 голосов
/ 20 мая 2018

Как отмечено в комментариях и предыдущем ответе, вы должны использовать 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}
        )
0 голосов
/ 20 мая 2018

Функция copy () создает поверхностную копию объекта.Здесь вам может понадобиться глубокая копия.

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

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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...