Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Take a list, sort by popularity and then remove duplicates [duplicate]

Tags:

python

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]
like image 879
Ricky Hewitt Avatar asked Feb 01 '11 11:02

Ricky Hewitt


People also ask

How do you sort in Excel and remove duplicates?

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.

How do you remove duplicates from a list in Python with for loop?

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.


1 Answers

>>> 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.

like image 187
SilentGhost Avatar answered Oct 14 '22 00:10

SilentGhost