Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to organize list by frequency of occurrence and alphabetically (in case of a tie) while eliminating duplicates?

Tags:

People also ask

How do you arrange a list of elements in alphabetical order?

Use the Python List sort() method to sort a list in place. The sort() method sorts the string elements in alphabetical order and sorts the numeric elements from smallest to largest. Use the sort(reverse=True) to reverse the default sort order.

Does sort function remove duplicates in Python?

Problems associated with sorting and removal of duplicates is quite common in development domain and general coding as well.


Basically if given a list:

data = ["apple", "pear", "cherry", "apple", "pear", "apple", "banana"]

I'm trying to make a function that returns a list like this:

["apple", "pear", "banana", "cherry"]

I'm trying to make the return list ordered by most frequently occurring word first while breaking ties by ordering them alphabetically. I also am trying to eliminate duplicates.

I've made lists already of the counts of each element and the indices of each element in data.

x = [n.count() for n in data]
z = [n.index() for n in data]

I don't know where to go from this point.