计算对象中 Json 数组中的出现次数

Count Occurence in Json Array within objects

我有以下 json,我试图从中计算 Python 中 拉丁美洲 等标签的出现次数。因为它出现了两次,所以它应该 return 2 for "Latin America" and 1 for "Mexico", "Health" and "Costa Rica".

{
"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)

我得到了所有标签列表的输出,但我一直在计算所有标签的计数。

你可以这样做

import json
from collections import Counter

with open('../../Resources/Aljazeera.json') as f:
   data = json.load(f)

all_tags = Counter()

for item in data['AlJazeera_data']:
    all_tags.update(item['tags']):

print(all_tags)

编辑:正如另一位发帖人指出的那样,不需要第二次调用 Counter

您需要.update()每个标签列表的计数器

tags = Counter()
for item in data['AlJazeera_data']:
    tags.update(item['tags'])

print(tags) # Counter({'Latin America': 2, 'Mexico': 1, 'Health': 1, 'Costa Rica': 1})
print(tags.most_common(1)) # [('Latin America', 2)]

total = sum(tags.values())
print(total) # 5

tags_percentage = {k: v/total for k,v in tags.items()}
print(tags_percentage) # {'Latin America': 0.4, 'Mexico': 0.2, 'Health': 0.2, 'Costa Rica': 0.2}