我想替換選項標簽和值輸入文本,我能夠訪問它們并更新它們,但它更新所有值,而不僅僅是一個標簽或輸入文本,而是所有索引。
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)
uj5u.com熱心網友回復:
不使用的東西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'}]}]
uj5u.com熱心網友回復:
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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/367490.html
上一篇:按索引(流)比較兩個字串陣列
