【发布时间】:2020-07-10 21:00:38
【问题描述】:
我从 API 调用接收返回 JSON,想要在检测到关键字时记录,有时可能从 API 返回一个、没有或多个。我能够记录返回的数据没有问题。
我想运行 1000 个请求,然后将每个结果记录为列表中的结果列表(所以我知道哪个列表对应于哪个 API 调用)。
for item in response['output']['keywords']:
TempEntityList = []
TempEntityList.append(item['keywords'])
EntityList.extend(TempEntityList)
TempEntityList = []
这确实将所有内容附加到列表中,但我似乎找不到正确的设置。
当我运行它两次时,我得到了以下内容。
['Chat', 'Case', 'Telephone','Chat', 'Case', 'Telephone']
当我真正想要的时候
[['Chat', 'Case', 'Telephone'],['Chat', 'Case', 'Telephone']]
我正在创建 TempEntityList 并将找到的任何匹配项附加到它,根据找到的内容扩展 EntityList,然后为下一次 API 调用清除 TempEntityList。
到目前为止,我只能将每组结果记录到嵌套列表中的最佳方法是什么,或者每个项目都是它自己的嵌套项目。
根据要求,返回的有效负载如下所示
{
"output": {
"intents": [],
"entities": [
{
"entity": "Chat",
"location": [
0,
4
],
"value": "Chat",
"confidence": 1
},
{
"entity": "Case",
"location": [
5,
9
],
"value": "Case",
"confidence": 1
},
{
"entity": "Telephone",
"location": [
10,
19
],
"value": "Telephony",
"confidence": 1
}
],
"generic": []
},
"context": {
"global": {
"system": {
"turn_count": 1
},
"session_id": "xxx-xxx-xxx"
},
"skills": {
"main skill": {
"user_defined": {
"Case": "Case",
"Chat": "Chat",
"Telephone": "Telephony"
},
"system": {
"state": "x"
}
}
}
}
}
{
"output": {
"intents": [],
"entities": [
{
"entity": "Chat",
"location": [
0,
4
],
"value": "Chat",
"confidence": 1
},
{
"entity": "Case",
"location": [
5,
9
],
"value": "Case",
"confidence": 1
},
{
"entity": "Telephone",
"location": [
10,
19
],
"value": "Telephony",
"confidence": 1
}
],
"generic": []
},
"context": {
"global": {
"system": {
"turn_count": 1
},
"session_id": "xxx-xxx-xxx"
},
"skills": {
"main skill": {
"user_defined": {
"Case": "Case",
"Chat": "Chat",
"Telephone": "Telephony"
},
"system": {
"state": "xxx-xxx-xxx"
}
}
}
}
}
{
"output": {
"intents": [],
"entities": [
{
"entity": "Chat",
"location": [
0,
4
],
"value": "Chat",
"confidence": 1
},
{
"entity": "Case",
"location": [
5,
9
],
"value": "Case",
"confidence": 1
},
{
"entity": "Telephone",
"location": [
10,
19
],
"value": "Telephony",
"confidence": 1
}
],
"generic": []
},
【问题讨论】:
标签: python arrays json python-3.x