繁体   English   中英

如何从 json 中提取数据并使用 python 为提取的值添加附加值?

[英]How to extract data from json and add additional values to the extracted values using python?

我想使用 python 解析来自 json 响应的值并将附加值分配给列表

{ "form": [{ "box": [60,120,260,115], "text": "hello", "label": "question", "words": [{ "box": [90,190,160,215 ],"text": "hello"} ], "linking": [[0,13]],"id": 0 }]}

我正在尝试使用 python 解析值并分配给变量。 我想要实现的是:如果实际的 output 是 ([60,120,260,115],hello) 我想在列表中添加更多值:因此预期 output 应该是:

([60,120,260,120,260,115,60,115],hello)

尝试这个:

tmp_json = { "form": [{ "box": [60,120,260,115], "text": "hello", "label": "question", "words": [{ "box": [90,190,160,215 ],"text": "hello"} ], "linking": [[0,13]],"id": 0 }]}
# Then do whatever you need to do with the list by accessing it as follows
# tmp_json["form"][0]["box"]

您可以在此处遍历列表的所有元素,如果每个项目都符合所需条件,则使用所需值扩展现有列表。

# Pseudocode
for item in data["form"]:
    # check each item's box attribute has all such elements i.e 60,120,260,115
    # AND item's text attribute has value "hello"
    # If matches then to add extra values to box list you can use <list>.extend([115, 120 etc])
    # e.g item["box"].extend([120, 115, 260])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM