Python 嵌套字典中的意外数据分配

Unexpected Data Assignment in Python Nested Dictionaries(Python 嵌套字典中的意外数据分配)

本文介绍了Python 嵌套字典中的意外数据分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Python 新手,所以请原谅任何愚蠢的错误,但经过研究,我无法弄清楚.我正在从日历中获取的月份中的天数列表中创建字典.我最初使用 dict.fromkeys() 但发现 这个 提交说服我改用我拥有的字典理解语句.然后我给字典中的每个值提供另一个字典,该字典以星期几作为键,另一个字典作为值.这本字典是 taskDic,它以 chores 为键,将人名作为值.

I am new to python so please excuse any dumb mistakes but after research, I can't figure this out. I am creating a dictionary from a list of days in the month taken from calendar. I had originally used dict.fromkeys() but found this submission that convinced me to change to the dictionary comprehension statement I have. Then I give each value in the dictionary another dictionary that has the day of the week as the key and another dictionary as the value. This dictionary is taskDic which has chores as the keys and will hold people's names as the values.

我的问题是我的循环中的最后一个语句是指派同一个人每天做垃圾(等),即使循环只是在第一天.我相信我启动字典的方式有问题,因为它为所有 dic 分配值,就好像它们是相同的一样.

My problem is that my last statement in my loops is assigning the same person to do trash (etc) for every day even though the loop is just on the first day. I believe there is something wrong with how I start the dictionary because it is assigning values to all the dics as if they are the same.

基本上我有与上面链接的问题相同的问题,但有嵌套字典.如果我需要澄清任何事情,请告诉我.谢谢!

Basically I have the same problem as the linked issue above but with nested dictionaries. Please let me know if I need to clarify anything. Thank you!

import calendar

week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
taskDic = {'Trash':[], 'Groceries':[], 'Clean':[]} 
teamList = ['Jane', 'Peter', 'Jake', 'Eliza', 'Sarah', 'Bill']
person = 0

cal = list(calendar.Calendar().itermonthdays(2015, 8))
cal = {k: {} for k in cal}

for i in cal:
    cal[i] = {week[i % 7]: taskDic}

for i in cal:  
     if (cal[i].keys() != 'Saturday') and (cal[i].keys() != 'Sunday'):
         for j in cal[i]:   
              for k in cal[i][j]:  
                cal[i][j][k] = teamList[person % len(teamList)]
                person += 1

我的结果是这样的:

0 {'Monday': {'Trash': 'Eliza', 'Groceries': 'Sarah', 'Clean': 'Bill'}}
1 {'Tuesday': {'Trash': 'Eliza', 'Groceries': 'Sarah', 'Clean': 'Bill'}}
2 {'Wednesday': {'Trash': 'Eliza', 'Groceries': 'Sarah', 'Clean': 'Bill'}}
3 {'Thursday': {'Trash': 'Eliza', 'Groceries': 'Sarah', 'Clean': 'Bill'}}
etc...

推荐答案

问题出在这里:

for i in cal:
    cal[i] = {week[i % 7]: taskDic}

您每天都在使用相同的 taskDic 副本.

you're using the same copy of taskDic every day.

正如@jojonas 所评论的,修复是这样的:

as commented on by @jojonas, a fix is this:

cal[i] = {week[i % 7]: taskDic.copy()}

唯一的问题是你最终会创建一堆未使用的副本,这对这个任务来说很好.

the only problem with this is that you'll end up creating a bunch of unused copies, which is fine for this task.

你想做的是:

week_of_tasks = [taskDic.copy() for _ in week]
cal[i] = {week[i % 7]: week_of_tasks[i % 7]}

这篇关于Python 嵌套字典中的意外数据分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Python 嵌套字典中的意外数据分配

基础教程推荐