Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Counting frequency in a list (See related posts)

>>> alist = [ '1', '1', '2', '1', '3', '4', '1', '3']
>>> [(a, alist.count(a)) for a in set(alist)]
[('1', 4), ('3', 2), ('2', 1), ('4', 1)]
>>> sorted(_, key=lambda x: -x[1])  # rank them
[('1', 4), ('3', 2), ('2', 1), ('4', 1)]

You need to create an account or log in to post comments to this site.


Click here to browse all 4861 code snippets

Related Posts