【问题标题】:appending to a dictionary附加到字典
【发布时间】:2018-10-23 05:28:17
【问题描述】:

我有一个这样的python代码:

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

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

【问题讨论】:

  • 它有效。非常感谢!

标签: python list dictionary


【解决方案1】:

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

conversation_dict['0'] = 10

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

【讨论】:

    猜你喜欢
    • 2023-02-24
    • 1970-01-01
    • 2015-06-10
    • 1970-01-01
    • 2012-02-14
    • 1970-01-01
    • 2018-12-27
    • 2020-08-07
    • 2022-10-17
    相关资源
    最近更新 更多