【问题标题】:Duplicate dictionary within a list列表中的重复字典
【发布时间】:2022-01-03 02:32:01
【问题描述】:

我想复制列表中的字典并将其附加到列表中。

foci=[
        {
          "label": "Monday 1:00 pm",
          "value": {
            "input": {
              "text": "Monday 1:00 pm"
            }
          }
        }
      ]

【问题讨论】:

    标签: json python-3.x list dictionary


    【解决方案1】:

    您需要deepcopy 嵌套字典并将其附加到列表中:

    from copy import deepcopy
    
    foci = [
        {
            "label": "Monday 1:00 pm",
            "value": {
                "input": {
                    "text": "Monday 1:00 pm"
                }
            }
        }
    ]
    
    foci.append(deepcopy(foci[0]))
    
    print(foci)
    

    【讨论】:

    • 如何制作多于 2 份的副本,谢谢您的回答。
    • 只需在循环中多次运行append 行 - 它每次都会在列表末尾添加一个副本。
    • 非常感谢您将在稍后将其标记为答案。
    猜你喜欢
    • 1970-01-01
    • 2019-09-14
    • 2019-06-07
    • 1970-01-01
    • 2022-11-27
    • 2019-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多