在python中为列表中的项生成词云

pyd*_*pyd 7 python string list cpu-word word-cloud

 my_list=["one", "one two", "three"]
Run Code Online (Sandbox Code Playgroud)

我正在使用这个列表生成一个文字云

 wordcloud = WordCloud(width = 1000, height = 500).generate(" ".join(my_list))
Run Code Online (Sandbox Code Playgroud)

当我将所有项目转换为字符串时,它正在生成单词云

   "one","two","three"

 But I want to generate word cloud for the values, "one","one two","three"
Run Code Online (Sandbox Code Playgroud)

帮助我为列表中的项目生成文字云

pyd*_*pyd 8

一种做法,

import matplotlib.pyplot as plt

#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()
Run Code Online (Sandbox Code Playgroud)

创建Counter Dictionary的另一种方法,

#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()
Run Code Online (Sandbox Code Playgroud)