How to count the frequency of the elements in an unordered list?
frequency, list, python
Solution
If the list is sorted, you can use `groupby` from the `itertools` standard library (if it isn't, you can just sort it first, although this takes O(n lg n) time):
from itertools import groupby
a = [5, 1, 2, 2, 4, 3, 1, 2, 3, 1, 1, 5, 2]
[len(list(group)) for key, group in groupby(sorted(a))]
Output:
[4, 4, 2, 1, 2]
Problem
Given an unordered list of values like ``` a = [5, 1, 2, 2, 4, 3, 1, 2, 3, 1, 1, 5, 2] ``` How can I get the frequency of each value that appears in the list, like so? ``` # `a` has 4 instances of `1`, 4 of `2`, 2 of `3`, 1 of `4,` 2 of `5` b = [4, 4, 2, 1, 2] # expected output ```