Posted By: Anonymous
I need to find the frequency of elements in an unordered list
a = [1,1,1,1,2,2,2,2,3,3,4,5,5]
b = [4,4,2,1,2]
Also I want to remove the duplicates from a
a = [1,2,3,4,5]
Note: You should sort the list before using
You can use
itertools package if the list is an ordered list.
a = [1,1,1,1,2,2,2,2,3,3,4,5,5] from itertools import groupby [len(list(group)) for key, group in groupby(a)]
[4, 4, 2, 1, 2]
update: Note that sorting takes O(n log(n)) time.