gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 21:38:45 26 4
gpt4 key购买 nike

 my_list=["one", "one two", "three"]

我正在为这个列表生成词云

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

当我将所有项目转换为字符串时,它正在为

生成词云
   "one","two","three"

But I want to generate word cloud for the values, "one","one two","three"

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

最佳答案

一种做法,

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

另一种方法是创建 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()

关于python - 在 python 中为列表中的项目生成词云,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45588724/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com