my_dict = { 'Khan': 4, 'Ali': 2, 'Luna': 6, 'Mark': 11, 'Pooja': 8, 'Sara': 1}
print('Original:')
print(my_dict)
my_dict.update({'Khan': 6, 'Luna': 9})
print('\nAfter update:')
print(my_dict)
my_dict = { 'Khan': 4, 'Ali': 2, 'Luna': 6, 'Mark': 11, 'Pooja': 8, 'Sara': 1}
for key, value in my_dict.items():
if key == 'Ali':
my_dict[key] = 10
print(my_dict)
my_dict = { 'Khan': 4, 'Ali': 2, 'Luna': 6, 'Mark': 11, 'Pooja': 8, 'Sara': 1}
my_dict = { **my_dict, 'Pooja': 12}
print(my_dict)
注:解包方法实际上是创建一个新的字典,而不是更新原来的字典。
注:推荐用解包方法修改,经测试,其他方法可能失败