I have a list of strings as:
string_list=['philadelphia court excessive disappointed court hope','hope jurisdiction obscures acquittal court','mention hope maryland signal held problem internal reform life bolster level grievance']
and a list of words as:
words=['hope','court','mention','maryland']
Now, all I want to get the count of list words occurance within list of strings into seperate dictionary with key as 'doc_(index) and values as nested dictionary with key as occured words and value as counts. Output expected as:
words_dict={'doc_1':{'court':2,'hope':1},'doc_2':{'court':1,'hope':1},'doc_3':{'mention':1,'hope':1,'maryland':1}}
what I did first step as:
docs_dict={}
count=0
for i in string_list:
count+=1
docs_dic['doc_'+str(count)]=i
print (docs_dic)
{'doc_1': 'philadelphia court excessive disappointed court hope', 'doc_2': 'hope jurisdiction obscures acquittal court', 'doc_3': 'mention hope maryland signal held problem internal reform life bolster level grievance'}
After this, I'm not able to get how I can get the word counts. What I did so far as:
docs={}
for k,v in words_dic.items():
split_words=v.split()
for i in words:
if i in split_words:
docs[k][i]+=1
else:
docs[k][i]=0