首页 > 解决方案 > 我使用 python 制作了一个 wordcloud,我想查看 wordcloud 中的单词,如有必要,将它们从图像中删除

问题描述

所以基本上,我有一个文件,其中有一些列表,我想用这些列表来查看这些单词是否存在于我给定的字典中,如果是,则从中形成一个 wordcloud。现在看了wordcloud之后,我想从图像中删除一些无用的单词,你们可以帮我吗?

from wordcloud import WordCloud
import matplotlib.pyplot as plt

import module1
import module2

lst = input('Which list?')


a = module2.diction

c = None
if lst == 'list1':
    c=module2.list1
if lst == 'list2':
    c=module2.list2

#print(c)
#doing stuff with the dictionary I had
for name, numb in a.items():
    for key in numb:
        key = key * numb[key]
        a[name] = key
#print(sample)
quad = ({key : a[key] for key in a if key in c})
#print(quad)

wc = WordCloud(background_color="white",width=1000,height=1000,colormap="copper", relative_scaling=0.5,
normalize_plurals=False).generate_from_frequencies(quad)

# Plot

plt.figure()
plt.ion()
plt.imshow(wc, interpolation="bilinear")
plt.margins(x=0, y=0)
plt.axis('off')
plt.tight_layout(pad=0)
plt.ioff()
plt.show()


u = input('Enter the words: ')

标签: python-3.xlistword-cloud

解决方案


如果要从字典中删除某些内容,可以使用以下语法:

del dictonary[word]

这将从字典中删除这个词,所以如果你遇到这个词the并想要删除它,你可以使用

del c['the']

推荐阅读