【问题标题】:Adding a list into a list in Python在 Python 中将列表添加到列表中
【发布时间】: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


【解决方案1】:

首先,由于TempEntityList = []for 循环的开头,您不需要在底部添加另一个TempEntityList = []。要回答这个问题,请使用list.append() 而不是list.extend()

for item in response['output']['keywords']:
    TempEntityList = []
    TempEntityList.append(item['keywords'])
    EntityList.append(TempEntityList)

【讨论】:

  • 我也尝试使用 append 分别给我每个项目 [['Chat'],['Case'],['Telephone']] 因为我不知道有多少(如果有的话) API 将返回的关键字我最终会得到一个很长的列表,而且我无法将它们与每个单独的调用相匹配。您对 TempEntityList 的看法是正确的,尽管这显然是一个失误,谢谢!
  • 你能举个例子吗?
  • @CallumK 显示响应的样子将帮助您在此处获得更好的答案。
  • 我现在已经从端点添加了响应,谢谢
  • 为什么不EntityList = [item['keywords']]
【解决方案2】:

我已经得到了我想要的,谢谢大家的建议。

解决办法是:

global EntityList
EntityList = []
for item in response['output']['entities']:
    EntityList.append(item['entity'])
    FinalList.append(EntityList)

在同一输入上运行该函数三次后产生的结果:

[['Chat', 'Case', 'Telephone'], ['Chat', 'Case', 'Telephone'], ['Chat', 'Case', 'Telephone']]

【讨论】:

    猜你喜欢
    • 2016-01-09
    • 2020-09-10
    • 2012-02-13
    • 1970-01-01
    • 2015-08-21
    • 2021-09-11
    • 1970-01-01
    • 1970-01-01
    • 2020-03-23
    相关资源
    最近更新 更多