Skip to content

Latest commit

 

History

History
14 lines (11 loc) · 410 Bytes

20220917084847.md

File metadata and controls

14 lines (11 loc) · 410 Bytes

use collections.Counter() to count things

collections.Counter() is one of my favorite Python data structures, it's so concise :)

>>> from collections import Counter
>>> languages = 'Python Java Perl Python JS C++ JS Python'.split()
>>> Counter(languages)
Counter({'Python': 3, 'JS': 2, 'Java': 1, 'Perl': 1, 'C++': 1})
>>> Counter(languages).most_common(2)
[('Python', 3), ('JS', 2)]

#collections