Reputation: 14614
I have a list of strings tags
which I wish to sort by count of appearances of the strings in the list.
I have tried:
Creating the list of unique strings,
uniqueTags = set(tags)
Then creating a second list with the counts for each of the unique string
countList = []
for items in uniqueTags:
countList.append(tags.count(items))
but then I am not sure how to sort.
Upvotes: 2
Views: 2130
Reputation: 34531
Use collections.Counter(...)
instead.
In [18]: from collections import Counter
In [19]: m = ['a', 'b', 'a', 'b', 'c']
In [20]: Counter(m).most_common()
Out[20]: [('a', 2), ('b', 2), ('c', 1)]
Counter.most_common()
returns a list of a tuple such that the first element is the string and the second is it's count and the list is ordered by the count.
In [21]: m2 = ['a', 'b', 'a', 'b', 'c', 'b']
In [22]: Counter(m2).most_common()
Out[22]: [('b', 3), ('a', 2), ('c', 1)]
Just to get a list of items, you could do
In [28]: [elem for elem, _ in Counter(m2).most_common()]
Out[28]: ['b', 'a', 'c']
If you're looking to sort the list you got, change your method to something like
In [23]: final_list = []
In [24]: for elem in set(m2):
...: final_list.append((elem, m2.count(elem)))
...:
In [25]: from operator import itemgetter
In [26]: sorted(final_list, key=itemgetter(1))
Out[26]: [('c', 1), ('a', 2), ('b', 3)]
In [27]: sorted(final_list, key=itemgetter(1), reverse=True)
Out[27]: [('b', 3), ('a', 2), ('c', 1)]
Upvotes: 6
Reputation: 43517
Here is one way to do it:
from collections import Counter
from operator import itemgetter
get_val = itemgetter(0)
def retrieve_unique_sorted_by_count(lst)
return [get_val(x) for x in Counter(lst).most_common()]
Upvotes: 1