不要使用groupby()(这需要对您的输入进行排序),而是使用collections.Counter();这不必为了计算输入而创建中间列表:
from collections import Counter
counts = Counter(a)
您还没有真正指定您认为是“直方图”的内容。假设您想在终端上执行此操作:
width = 120 # Adjust to desired width
longest_key = max(len(key) for key in counts)
graph_width = width - longest_key - 2
widest = counts.most_common(1)[0][1]
scale = graph_width / float(widest)
for key, size in sorted(counts.items()):
print('{}: {}'.format(key, int(size * scale) * '*'))
演示:
>>> from collections import Counter
>>> a = ['a', 'a', 'a', 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'e', 'e', 'e', 'e', 'e']
>>> counts = Counter(a)
>>> width = 120 # Adjust to desired width
>>> longest_key = max(len(key) for key in counts)
>>> graph_width = width - longest_key - 2
>>> widest = counts.most_common(1)[0][1]
>>> scale = graph_width / float(widest)
>>> for key, size in sorted(counts.items()):
... print('{}: {}'.format(key, int(size * scale) * '*'))
...
a: *********************************************************************************************
b: **********************************************
c: **********************************************************************
d: ***********************
e: *********************************************************************************************************************
在numpy.histogram() 和matplotlib.pyplot.hist() 函数中可以找到更复杂的工具。这些为您计算,matplotlib.pyplot.hist() 还为您提供图形输出。