在python中为列表中的项生成word cloud

在python中为列表中的项生成word cloud,python,string,list,word,word-cloud,Python,String,List,Word,Word Cloud,我正在使用 my_list=["one", "one two", "three"] 当我将所有项目转换为字符串时,它正在为其生成单词cloud wordcloud = WordCloud(width = 1000, height = 500).generate(" ".join(my_list)) 帮助我为列表中的项目生成word cloudWordCloud以正则表达式作为参数。使用此选项,我们可以将拆分字符设置为+而不是空格 "one","two","three" But

我正在使用

 my_list=["one", "one two", "three"]
当我将所有项目转换为字符串时,它正在为其生成单词cloud

 wordcloud = WordCloud(width = 1000, height = 500).generate(" ".join(my_list))

帮助我为列表中的项目生成word cloud

WordCloud
以正则表达式作为参数。使用此选项,我们可以将拆分字符设置为
+
而不是空格

   "one","two","three"

 But I want to generate word cloud for the values, "one","one two","three"
然后,单词列表需要在
+
上连接,每个单词现在都用于拆分单词。产生以下代码:

regexp=r"\w[\w' ]+"
一种做法是

wordcloud = WordCloud(width=1000, height=500, regexp=r"\w[\w' ]+").generate("+".join(my_list))
另一种方法是创建计数器字典

import matplotlib.pyplot as plt
from wordcloud import WordCloud

#convert list to string and generate
unique_string=(" ").join(my_list)
wordcloud = WordCloud(width = 1000, height = 500).generate(unique_string)
plt.figure(figsize=(15,8))
plt.imshow(wordcloud)
plt.axis("off")
plt.savefig("your_file_name"+".png", bbox_inches='tight')
plt.show()
plt.close()
我得到“keyrerror:”
#convert it to dictionary with values and its occurences
from collections import Counter
word_could_dict=Counter(my_list)
wordcloud = WordCloud(width = 1000, height = 500).generate_from_frequencies(word_could_dict)

plt.figure(figsize=(15,8))
plt.imshow(wordcloud)
plt.axis("off")
#plt.show()
plt.savefig('yourfile.png', bbox_inches='tight')
plt.close()