擅长:python、mysql、java
<p>如果我没听错的话,那么要得到一个值为键<strong>“1”</strong>的字典,您需要的也是一个字典:</p>
<pre><code>test_dict = {1: {45:"t"}, 2:{44:"tt"}}
new_value = {1: {44: "pae"}}
update_value = {1:{44:"tt"} }
def add_to_dict(dict, new_value):
for key, value in new_value.items():
dict[key].update(value)
# print(new_value.keys())
def update_dict(dict,new_value):
dict.update(new_value)
if _name_ == '_main_':
add_to_dict(test_dict,new_value)
print(test_dict)
update_dict(test_dict,update_value)
print(test_dict)
</code></pre>
<p>它将打印:</p>
<pre><code># after you add a value to the dictionary inside the dictionary
{1: {44: 'pae', 45: 't'}, 2: {44: 'tt'}}
# after you update the value
{1: {44: 'tt'}, 2: {44: 'tt'}}
</code></pre>