首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将输出值转换为字典

如何将输出值转换为字典
EN

Stack Overflow用户
提问于 2019-07-24 13:34:58
回答 1查看 28关注 0票数 1

Text =“权力的游戏”是一部由大卫·贝尼奥夫( David Benioff )和D·B·魏斯(D.B. Weiss )为HBO制作的美国奇幻剧。这部电视剧是在英国贝尔法斯特制作和拍摄的。

代码语言:javascript
复制
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)

我的输出

代码语言:javascript
复制
and
D
Benioff
and
filmed
produced
in
Belfast
filmed
in
the
elsewhere

我想要的输出

代码语言:javascript
复制
{and:['D','Benioff','filmed','produced'],
in:['Belfast','filmed','the','elsewhere']}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-24 13:45:26

这是一种使用dict的方法。

演示:

代码语言:javascript
复制
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)

输出:

代码语言:javascript
复制
{'and': ['D.', 'Benioff', 'filmed', 'produced'],
 'in': ['Belfast', 'filmed', 'the', 'elsewhere']}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57184244

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档