[英]Iterating through nested dictionaries and find the keywords in the value of dictionary in python
我有以下格式的數據。
data = {"policy": {"1": {"ID": "ML_0", "URL": "www.a.com", "Text": "my name is Martin and here is my code"} "2": {"ID": "ML_1", "URL": "www.b.com", "Plain_Text" my name is Mikal and here is my code"}}}
keywords = ['is', 'my']
以下是我想在 python 中處理我的數據的幾件事。
首先遍歷我的字典並在“1”和“2”中的“Text”值中查找並計算上述關鍵字,最后一件事是用關鍵字計數更新當前字典(在“1”和“2”如下所示。
{"policy": {"1": {"ID": "ML_0", "URL": "www.a.com", "Text": "my name is Martin and here is my code", "is": "2", "my": "2"} "2": {"ID": "ML_1", "URL": "www.b.com", "Plain_Text: "my name is Mikal and here is my code", "is": "2", "my": "2"}}}
如果有人可以幫助我,將不勝感激。
你可以使用collections. Counter
collections. Counter
:
from collections import Counter
import json # Only for pretty printing `data` dictionary.
def get_keyword_counts(text: str, keywords: list[str]) -> dict[str, int]:
return {
word: count for word, count in Counter(text.split()).items()
if word in set(keywords)
}
def main() -> None:
data = {
"policy": {
"1": {
"ID": "ML_0",
"URL": "www.a.com",
"Text": "my name is Martin and here is my code"
},
"2": {
"ID": "ML_1",
"URL": "www.b.com",
"Text": "my name is Mikal and here is my code"
}
}
}
keywords = ['is', 'my']
for policy in data['policy'].values():
policy |= get_keyword_counts(policy['Text'], keywords)
print(json.dumps(data, indent=4))
if __name__ == '__main__':
main()
輸出:
{
"policy": {
"1": {
"ID": "ML_0",
"URL": "www.a.com",
"Text": "my name is Martin and here is my code",
"my": 2,
"is": 2
},
"2": {
"ID": "ML_1",
"URL": "www.b.com",
"Text": "my name is Mikal and here is my code",
"my": 2,
"is": 2
}
}
}
注意:使用|=
來合並 dicts 是 Python 3.10 的一個特性。 如果您使用的是舊版本,應該不難谷歌如何做到這一點。
(我沒有足夠的聲譽發表評論,所以我將作為答案發布)首先,我認為您的 dict 結構一開始就不正確。 語法似乎不正確
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.