簡體   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