用重复的键替换字典列表中的值,即使更新了一个索引,它也会更新两个索引

Replace values in a list of dictionaries with duplicate keys, it updates both indexes even if one is updated

我想替换选项 label 和值 input text,我能够访问它们并更新它们,但它更新了所有值不仅是一个 labelinput text 而是所有 indexes.

   response=[ {'arr': [{'title': 'Wählen Sie das passende Rechtsgebiet:', 
    'options': [{'label': 'Monday 1:00 pm', 'value': {'input': {'text': 'Monday 1:00 pm'}}}, 
    {'label': 'Monday 1:00 pm', 'value': {'input': {'text': 'Monday 1:00 pm'}}}], 
    'description': '', 'response_type': 'option'}]}]

我更新字典值的代码有效,但它用相同的值更新了两个索引:

response['arr'][0]['options'][0].update({'label': "Contract Law"})
response['arr'][0]['options'][0]['value']['input'].update({'text': "Contract Law"})
response['arr'][0]['options'][1].update({'label': "Sales law"})
response['arr'][0]['options'][1]['value']['input'].update({'text': ""Sales law""})

结果:

完整代码:

import numpy as np
list_foci=["Erbschaftssteuerrecht","Erbrecht"]
list_size=len(list_foci)
dynamic_list= [
    {
      "title": "What time do you want your appointment?",
      "options": [
        {
          "label": "Monday 1:00 pm",
          "value": {
            "input": {
              "text": "Monday 1:00 pm"
            }
          }
        }
      ],
      "description": "",
      "response_type": "option"
    }
  ]
#for creating new values from the first index options e.g. label,value etc....
updated=list(np.repeat(dynamic_list[0]['options'], list_size))
response={
  "arr": [
    {
      "title": "Wählen Sie das passende Rechtsgebiet:",
      "options": updated,
      "description": "",
      "response_type": "option"
    }
  ]
  }
response['arr'][0]['options'][0]['label']= "Contract Law"
response['arr'][0]['options'][0]['value']['input']['text'] = "Contract Law"
response['arr'][0]['options'][1]['label']= "Sales Law"
response['arr'][0]['options'][1]['value']['input']['text'] = "Sales Law"
print(response)

不使用 update() 的东西呢?

response[0]["arr"][0]["options"][1]["value"]["input"]["text"] = "Contract Law"

结果:

In [1]: response
Out[1]: 
[{'arr': [{'title': 'Wählen Sie das passende Rechtsgebiet:',
    'options': [{'label': 'Monday 1:00 pm',
      'value': {'input': {'text': 'Monday 1:00 pm'}}},
     {'label': 'Monday 1:00 pm',
      'value': {'input': {'text': 'Contract Law'}}}],
    'description': '',
    'response_type': 'option'}]}]
from copy import deepcopy
list_foci=["Erbschaftssteuerrecht","Erbrecht"]
list_size=len(list_foci)
dynamic_list={
  "arr": [
    {
      "title": "What time do you want your appointment?",
      "options": "foci",
      "description": "",
      "response_type": "option"
    }
  ]
}
foci=[
    {
        "label": "Monday 1:00 pm",
        "value": {
            "input": {
                "text": "Monday 1:00 pm"
            }
        }
    }
]
copy_size=list_size-1
for x in range(copy_size):
    foci.append(deepcopy(foci[0]))
print(copy_size)
for foci_value in range(list_size):
    foci[foci_value]['label']=list_foci[foci_value]
    foci[foci_value]['value']['input']['text']=list_foci[foci_value]
print(foci)