无法更新嵌套字典中的单个值
Can't update a single value in a nested dictionary
在创建像 {'key': {'key': {'key': 'value'}}}
这样的字典后,我 运行 遇到了尝试为更高深度键设置值的问题。更新其中一个值后,(其他键的)剩余值的值也被更新。
这是我的 Python 代码:
times = ["09:00", "09:30", "10:00", "10:30"]
courts = ["1", "2"]
daytime_dict = dict.fromkeys(times)
i = 0
for time in times:
daytime_dict[times[i]] = dict.fromkeys(["username"])
i += 1
courts_dict = dict.fromkeys(courts)
k = 0
for court in courts:
courts_dict[courts[k]] = daytime_dict
k += 1
day_info = [('name', '09:00', 1), ('name', '09:30', 1)]
for info in day_info:
info_court = str(info[2])
time = info[1]
# Here I am trying to set the value for courts_dict['1']['09:00']["username"] to be 'name',
# but the value for courts_dict['2']['09:00']["username"] and courts_dict['3']['09:00']["username"] is also set to 'name'
# What am I doing wrong? How can I only update the value for where the court is '1'?
courts_dict[info_court][time]["username"] = info[0]
我想得到这个:
{'1': {'09:00': {'username': 'name'},
'09:30': {'username': 'name'},
'10:00': {'username': None},
'10:30': {'username': None}},
'2': {'09:00': {'username': None},
'09:30': {'username': None},
'10:00': {'username': None},
'10:30': {'username': None}}
但我明白了:
{'1': {'09:00': {'username': 'name'},
'09:30': {'username': 'name'},
'10:00': {'username': None},
'10:30': {'username': None}},
'2': {'09:00': {'username': 'name'},
'09:30': {'username': 'name'},
'10:00': {'username': None},
'10:30': {'username': None}}
(看看 court_dict['2']['09:00']['username']
和 court_dict['2']['09:30']['username']
是如何同时更新的,而我只想更新来自 court_dict['1']
的值)
从逻辑上讲,我无法理解为什么在更新 courts_dict
时会更新两个值(我在最后一行代码中的做法),而不是只更新一个。由于 info_court
是 "1"
,我认为只有那个法院的 "username"
会被更新。
我做错了什么?
Logically, I can't understand why both values are updated when I update the courts_dict
对于您正在使用的字典对象,您正在分配相同的对象 references 作为值,因此您看到“两个值都已更新”的原因。您可能想使用 copy
或 deepcopy
:
重新编写代码
https://docs.python.org/3/library/copy.html
Assignment statements in Python do not copy objects, they create bindings between a target and an object. For collections that are mutable or contain mutable items, a copy is sometimes needed so one can change one copy without changing the other.
在创建像 {'key': {'key': {'key': 'value'}}}
这样的字典后,我 运行 遇到了尝试为更高深度键设置值的问题。更新其中一个值后,(其他键的)剩余值的值也被更新。
这是我的 Python 代码:
times = ["09:00", "09:30", "10:00", "10:30"]
courts = ["1", "2"]
daytime_dict = dict.fromkeys(times)
i = 0
for time in times:
daytime_dict[times[i]] = dict.fromkeys(["username"])
i += 1
courts_dict = dict.fromkeys(courts)
k = 0
for court in courts:
courts_dict[courts[k]] = daytime_dict
k += 1
day_info = [('name', '09:00', 1), ('name', '09:30', 1)]
for info in day_info:
info_court = str(info[2])
time = info[1]
# Here I am trying to set the value for courts_dict['1']['09:00']["username"] to be 'name',
# but the value for courts_dict['2']['09:00']["username"] and courts_dict['3']['09:00']["username"] is also set to 'name'
# What am I doing wrong? How can I only update the value for where the court is '1'?
courts_dict[info_court][time]["username"] = info[0]
我想得到这个:
{'1': {'09:00': {'username': 'name'},
'09:30': {'username': 'name'},
'10:00': {'username': None},
'10:30': {'username': None}},
'2': {'09:00': {'username': None},
'09:30': {'username': None},
'10:00': {'username': None},
'10:30': {'username': None}}
但我明白了:
{'1': {'09:00': {'username': 'name'},
'09:30': {'username': 'name'},
'10:00': {'username': None},
'10:30': {'username': None}},
'2': {'09:00': {'username': 'name'},
'09:30': {'username': 'name'},
'10:00': {'username': None},
'10:30': {'username': None}}
(看看 court_dict['2']['09:00']['username']
和 court_dict['2']['09:30']['username']
是如何同时更新的,而我只想更新来自 court_dict['1']
的值)
从逻辑上讲,我无法理解为什么在更新 courts_dict
时会更新两个值(我在最后一行代码中的做法),而不是只更新一个。由于 info_court
是 "1"
,我认为只有那个法院的 "username"
会被更新。
我做错了什么?
Logically, I can't understand why both values are updated when I update the courts_dict
对于您正在使用的字典对象,您正在分配相同的对象 references 作为值,因此您看到“两个值都已更新”的原因。您可能想使用 copy
或 deepcopy
:
https://docs.python.org/3/library/copy.html
Assignment statements in Python do not copy objects, they create bindings between a target and an object. For collections that are mutable or contain mutable items, a copy is sometimes needed so one can change one copy without changing the other.