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())]

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 (word)
            suffix_word =  split_text[index + 1]
            #print (suffix_word)
            prefix_word =  split_text[index - 1]
            #print (prefix_word)
            result[word].extend([suffix_word, prefix_word])  #Use list.extend to add to result.
            #print (result[word])
print(result)

我当前的输出如下:

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

我想要的输出如下:

{'and': 
     {suffix_word:['D.', 'filmed'], 
     prefix_word: ['Benioff', 'produced']}
'in': 
    {suffix_word:['Belfast', 'the'], 
    prefix_word: ['filmed','elsewhere']}}
分析解答

你快到了只需稍微更改您的代码即可实现:

import re
from collections import Counter

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.'''

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: {"suffix_word": [], "prefix_word": []} 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:
            suffix_word =  split_text[index + 1] if index < len(split_text)-1 else ""
            prefix_word =  split_text[index - 1] if index > 0 else ""
            result[word]["suffix_word"].append(suffix_word)
            result[word]["prefix_word"].append(prefix_word)

print(result)

输出:

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