【发布时间】:2020-05-23 12:20:07
【问题描述】:
我有下面的 json,我试图从中计算 Python 中 Latin America 等标签的出现次数。由于它出现了两次,它应该为“Latin America”返回 2,为“Mexico”、“Health”和“Costa Rica”返回 1。
{
"AlJazeera_data": [
{
"name": "Mexico City hospitals reaching breaking point",
"url": "https://www.aljazeera.com/news/",
"tags": [
"Latin America",
"Mexico",
"Health"
],
"author": "Manuel Rapalo"
},
{
"name": "Football matches resume in Costa Rica as virus curbs ease",
"url": "https://www.aljazeera.coml",
"tags": [
"Latin America",
"Costa Rica"
],
"author": "Manuel Rapalo"
}]
}
使用此代码:
import json
from collections import Counter
with open('../../Resources/Aljazeera.json') as f:
data = json.load(f)
for item in data['AlJazeera_data']:
for t in item['tags']:
print(t)
我得到了所有标签列表的输出,但我一直在计算所有标签的计数。
【问题讨论】:
-
如果你有这些物品,为什么不把它们放在一个清单上,或者更好
collections.Counter
标签: python json count find-occurrences