附加到字典

时间:2018-10-23 05:28:17

标签: python list dictionary

我有这样的python代码:

experiment_str = ['0', '1', '2', '3', '4', '5']
conversation_dict = dict.fromkeys(experiment_str,[])
conversation_dict['0'].append(10)

我只想将10添加到第一个键,但是将它添加到所有键。您能帮我理解吗?

1 个答案:

答案 0 :(得分:0)

对于字典,您不应该使用.append方法,只需使用'=':

conversation_dict['0'] = 10

此外,如果要创建新密钥,请使用相同的方法。

相关问题