要修改字典中的value值,可以通過以下幾種方式:
示例代碼:
my_dict = {'key1': 'value1', 'key2': 'value2'}
my_dict['key1'] = 'new value1'
print(my_dict) # 輸出: {'key1': 'new value1', 'key2': 'value2'}
示例代碼:
my_dict = {'key1': 'value1', 'key2': 'value2'}
my_dict.update({'key1': 'new value1', 'key2': 'new value2'})
print(my_dict) # 輸出: {'key1': 'new value1', 'key2': 'new value2'}
示例代碼:
my_dict = {'key1': 'value1', 'key2': 'value2'}
new_dict = {key: 'new ' + value for key, value in my_dict.items()}
print(new_dict) # 輸出: {'key1': 'new value1', 'key2': 'new value2'}
請根據具體需求選擇適合的方法來修改字典的value值。