Checking number of elements in Python's `Counter`

半腔热情 提交于 2021-01-27 05:43:09

问题


Python 2.7/3.1 introduced the awesome collections.Counter.

My question: How do I count how many "element appearances" a counter has?

I want this:

len(list(counter.elements()))

But shorter.


回答1:


sum(counter.itervalues())



来源:https://stackoverflow.com/questions/5506359/checking-number-of-elements-in-pythons-counter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!