【发布时间】:2016-04-16 13:16:06
【问题描述】:
我已将集合模块中的 Counter 函数应用于列表。在我这样做之后,我并不完全清楚新数据结构的内容将被描述为什么。我也不确定访问元素的首选方法是什么。
我做过类似的事情:
theList = ['blue', 'red', 'blue', 'yellow', 'blue', 'red']
newList = Counter(theList)
print newList
返回:
Counter({'blue': 3, 'red': 2, 'yellow': 1})
如何访问每个元素并打印出如下内容:
blue - 3
red - 2
yellow - 1
【问题讨论】:
标签: python list collections counter