在python中向列表添加元素
我有以下列表-在python中向列表添加元素,python,json,list,Python,Json,List,我有以下列表-params params [{'vehicles': [{'images': [], 'id': 55}], 'dealerId': 12345}] >>> type(params) <type 'list'> 我真的很感激任何帮助 注意:params是列表类型,而不是json您可以这样做: images = params[0]["vehicles"]["images"] images.append({ "imageId": 91,
params
params
[{'vehicles': [{'images': [], 'id': 55}], 'dealerId': 12345}]
>>> type(params)
<type 'list'>
我真的很感激任何帮助
注意:
params
是列表类型,而不是json您可以这样做:
images = params[0]["vehicles"]["images"]
images.append({
"imageId": 91,
"imageUrl":"file://image1.jpg"
})
images.append({
"imageId": 92,
"imageUrl":"file://image2.jpg"
})
只需访问图像列表,然后在字典中添加所需信息
params[0]["vehicles"][0]["images"].append({"imageId":91, "imageUrl":"file://image1.jpg"})
要实现此解决方案,请考虑您的列表,params。您想要编辑与键“图像”关联的列表
到达正确的列表后,您可以根据需要追加/编辑。您的列表是否包含要更新的多个元素?你试过什么吗?你到底有什么问题?你到底试过什么?“所以,这毕竟不是一个免费的编码服务。”我知道我应该更详细一些。对不起,我没有太多时间。你可以在下面答案的评论部分找到你问题的答案。干杯谢谢@ndlawrence。我也在做同样的事情,但没有使用字符串“vehicles”。相反,我只是使用索引号。
params[0]["vehicles"][0]["images"].append({"imageId":91, "imageUrl":"file://image1.jpg"})
# Gives the first dictionary containing the keys "vehicles" and "dealerId"
params[0]
# Gives the list containing the dictionary containing the keys "images" and "id"
params[0]["vehicles"]
# Gives the dictionary containing the keys "images" and "id"
params[0]["vehicles"][0]
# Gives the list associated with the key "images"
params[0]["vehicles"][0]["images"]