我有一大串单词,比如['abc', 'def', 'python', 'abc', 'python', ...]
{'python': 10, 'abc': 8, 'def': 2,...}发布于 2015-10-28 01:05:42
collections.Counter提供了一种方便和相对快速的方法来创建像您展示的字典:
from collections import Counter
x = ['spam', 'ham', 'eggs', 'ham', 'chips', 'eggs', 'spam', 'spam', 'spam']
counts = Counter(x)
print(counts)
# Counter({'spam': 4, 'eggs': 2, 'ham': 2, 'chips': 1})要将计数可视化,可以使用matplotlib条形图:
from matplotlib import pyplot as plt
import numpy as np
# sort counts in descending order
labels, heights = zip(*sorted(((k, v) for k, v in counts.items()), reverse=True))
# lefthand edge of each bar
left = np.arange(len(heights))
fig, ax = plt.subplots(1, 1)
ax.bar(left, heights, 1)
ax.set_xticks(left + 0.5)
ax.set_xticklabels(labels, fontsize='large')

发布于 2015-10-27 14:34:23
你可以得到一个单词计数:
lst = ['abc', 'def', 'python', 'abc', 'python']
wordcount = {}
for word in lst:
wordcount.setdefault(word,0)
wordcount[word] += 1实际上,使用python:https://plot.ly/python/histograms/创建图表似乎是上帝的选择。
https://stackoverflow.com/questions/33370669
复制相似问题