I'm trying to run the following code which counts the keywords in the specific value of the dictionary but it always shows me TypeError: 'type' object is not subscriptableas marked the error in the code as well. can someone please check and help me to solve this issue. Thanks
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)
}
// TypeError: 'type' object is not subscriptable
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()
TypeError), on Python 3.9.list[str]only became supported in Python 3.9. I'm guessing you are on an older version of Python. You can usetyping.List/typing.Dict/etc. instead.def get_keyword_counts(text: str, keywords: list[str]) -> dict[str, int]: