user6882757
user6882757

Reputation:

How to convert output values into dictionary

Text = '''Game of Thrones is an American fantasy drama television series created by David Benioff and D. B. Weiss for HBO. The show was both produced and filmed in Belfast elsewhere in the United Kingdom.'''

import re
new_text = ''
punctuations = '''!.,'''
for character in Text:
    if character not in punctuations:
        new_text = new_text + character
from collections import Counter
split_text = Text.split(' ')
count = Counter(split_text)
most_freq_word_new = [key for key,valu in count.items() if valu == max(count.values())]
for index, word in enumerate(split_text):
    for i in most_freq_word_new:
        if word == i:
            print (word)
            #print (index)
            suffix_word =  split_text[index + 1]
            prefix_word =  split_text[index - 1]
            print (suffix_word)
            print (prefix_word)

My output

and
D
Benioff
and
filmed
produced
in
Belfast
filmed
in
the
elsewhere

My desired output

{and:['D','Benioff','filmed','produced'],
in:['Belfast','filmed','the','elsewhere']}

Upvotes: 1

Views: 31

Answers (1)

Rakesh
Rakesh

Reputation: 82765

This is one approach using a dict.

Demo:

Text = '''Game of Thrones is an American fantasy drama television series created by David Benioff and D. B. Weiss for HBO. The show was both produced and filmed in Belfast elsewhere in the United Kingdom.'''
import re
new_text = ''
punctuations = '''!.,'''
for character in Text:
    if character not in punctuations:
        new_text = new_text + character
from collections import Counter, defaultdict
split_text = Text.split(' ')
count = Counter(split_text)
most_freq_word_new = [key for key,valu in count.items() if valu == max(count.values())]

result =  {i: [] for i in most_freq_word_new}     #Create Dict with word as key and list as value
for index, word in enumerate(split_text):
    for i in most_freq_word_new:
        if word == i:
            #print (index)
            suffix_word =  split_text[index + 1]
            prefix_word =  split_text[index - 1]
            result[word].extend([suffix_word, prefix_word])  #Use list.extend to add to result. 
print(result)

Output:

{'and': ['D.', 'Benioff', 'filmed', 'produced'],
 'in': ['Belfast', 'filmed', 'the', 'elsewhere']}

Upvotes: 1

Related Questions