【问题标题】:How to add a 1d Array to a Dictionary of 2d Arrays for a Specific Dictionary Key?如何将一维数组添加到特定字典键的二维数组字典中?
【发布时间】:2019-11-09 18:53:35
【问题描述】:

我有一个字典,其中每个值都是 2D 列表,我想将 1D 列表添加到特定键。例如,如何将[3, 7.0] 添加到此字典示例中的条目 1 中?

 # Original dictionary
 dictionary = {1: [[2, 5.95], [3, 4.72]], 2: [[1, 5.95], [5,3.52]]}

 # Dictionary after adding [3, 7.0] to entry 1
 dictionary = {1: [[2, 5.95], [3, 4.72], [3,7.0]], 2: [[1, 5.95], [5,3.52]]}

【问题讨论】:

  • dictionary[1].append([3, 7.0])?

标签: python arrays list dictionary


【解决方案1】:

在python中使用字典索引

dictionary = {1: [[2, 5.95], [3, 4.72]], 2: [[1, 5.95], [5,3.52]]}
dictionary[1].append([3,7.0])

print(dictionary)
>> {1: [[2, 5.95], [3, 4.72], [3,7.0]], 2: [[1, 5.95], [5,3.52]]}

【讨论】:

    猜你喜欢
    • 2020-03-19
    • 1970-01-01
    • 1970-01-01
    • 2021-12-02
    • 1970-01-01
    • 1970-01-01
    • 2018-08-19
    • 1970-01-01
    • 2019-07-23
    相关资源
    最近更新 更多