Possible Duplicate:
In python, how do I take the highest occurrence of something in a list, and sort it that way?
Hi all,
I am looking for an easy way to sort a list by popularity and then remove duplicate elements.
For example, given a list:
[8, 8, 1, 1, 5, 8, 9]
I would then end up with a list like the following:
[8, 1, 5, 9]
In Excel, there are several ways to filter for unique values—or remove duplicate values: To filter for unique values, click Data > Sort & Filter > Advanced. To remove duplicate values, click Data > Data Tools > Remove Duplicates.
You can make use of a for-loop that we will traverse the list of items to remove duplicates. The method unique() from Numpy module can help us remove duplicate from the list given. The Pandas module has a unique() method that will give us the unique elements from the list given.
>>> lst = [1, 1, 3, 3, 5, 1, 9]
>>> from collections import Counter
>>> c = Counter(lst)
>>> [i for i, j in c.most_common()]
[1, 3, 5, 9]
see collections.Counter
docs for the links to legacy versions-compatible implementations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With