用Python解析列表

我正在尝试在Python中使用 MONKEYLEARN API 进行情感分析。

当我打印结果时,得到以下响应。

[{'text': 'Today is a very good weather','external_id': None,'error': False,'classifications': [{'tag_name': 'Positive','tag_id':60333048,'confidence': 0.987}]}]

如果类型为list的结果,并且我只想从此列表中将分类结果隔离出来

TAG_NAME:肯定信心:0.987

GFHJTYHTR 回答:用Python解析列表

说您正在得到这样的响应:

lst = [{'text': 'Today is a very good weather','external_id': None,'error': False,'classifications': [{'tag_name': 'Positive','tag_id': 60333048,'confidence': 0.987}]}]

执行以下操作:

classification_result = lst[0]['classifications'][0]
tag_name = classification_result['tag_name']
confidence = classification_result['confidence']
,
    result = [{'text': 'Today is a very good weather','confidence': 0.987}]}]
    classification_result = result[0].get("classifications")[0]

现在这是字典,可以从中收集以下内容:

  • 使用分类结果[“标记名”]的标记名
  • 使用category_result [“ tag_id”]
  • 的tag_id
  • 使用category_result [“ confidence”]

它是列表内的嵌套字典,而字典内是列表

,

只需从列表中获取您要的元素,然后选择classification。 如果您打印列表,则可以轻松做到这一点。


your_list = [{'text': 'Today is a very good weather','tag_id':60333048,'confidence': 0.987}]}]
print(your_list)
#output
[{'text': 'Today is a very good weather','confidence': 0.987}]}]

然后执行

your_list[0]['classifications']
,

所有答案均有效。 我接受了AVION和Sayandip Dutta的回答。

classification_result = lst[0]['classifications'][0]
tag_name = classification_result['tag_name']
confidence = classification_result['confidence']
本文链接:https://www.f2er.com/3153051.html

大家都在问