我写了以下代码,这些代码将一组不同新闻网站的新闻标题中的最上面的单词吐出来:
... # list of headline words is in finale
filtered_word_list = finale[:] #make a copy of the word_list
for word in finale: # iterate over word_list
if word in stopwords.words('english'):
filtered_word_list.remove(word) # remove word from filtered_word_list if it is a stopword
filtlist = str(Counter(filtered_word_list))
line = re.sub('[!@#$-]', '', filtlist)
print(line)
当我试图通过以下方式来绘制:
^{pr2}$我得到以下错误:
ValueError: could not convert string to float: 'Counter({**BIG LONG LIST OF WORDS IT FOUND THAT WOULD MAKE THIS PAGE UNREADABLE**})
/usr/local/lib/python3.4/dist-packages/matplotlib/backends/backend_gtk3.py:215: Warning: Source ID 7 was not found when attempting to remove it
GLib.source_remove(self._idle_event_id)
我对如何正确使用matplotlib
(以及其中的pyplot
模块)知道如何将它吐出的Counter dict对象转换为matplotlib
可以用pyplot
绘图的dict对象。在
你们有谁知道我该如何着手解决这个问题吗?在
您可以使用
list(Counter...)
将counter对象转换为一个列表。您可以使用Counter(...).items()
、keys()
和values()
来获取项、键和值。它只是一本字典。在一种可能的解决方案是使用
re.sub
而不将Counter
转换为字符串,为此使用Counter.keys
。在为了绘图,您可以使用
Counter
并将其转储到pandas
DF中。在如果您将
Counter
保留为collections.Counter
类,那么绘图会更容易。你遇到的问题是你试图绘制一个str
。在相关问题 更多 >
编程相关推荐