【问题标题】:How to update the list which is having dictionary data?如何更新包含字典数据的列表?
【发布时间】:2020-12-07 13:16:25
【问题描述】:

这是包含字典数据的列表:

Fruits = [{'Apple':'10','Banana':'20','Orange':'40'}]

我需要通过添加 'Grapes':'60' 来更新列表。

预期输出

Fruits = [{'Apple':'10','Banana':'20','Orange':'40','Grapes':'60'}]

我该怎么做? 谢谢。

【问题讨论】:

    标签: python list dictionary append keyvaluepair


    【解决方案1】:

    假设列表中有一个dict,你可以通过索引访问它并添加适当的键值对:

    Fruits = [{'Apple':'10','Banana':'20','Orange':'40'}]
    
    # add 'Grapes': '60' to existing dict    
    Fruits[0]['Grapes'] = '60'
    
    print(Fruits)
    

    输出:

    [{'Apple': '10', 'Banana': '20', 'Orange': '40', 'Grapes': '60'}] 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多