Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find most common elements of a list? [duplicate]

Given the following list

['Jellicle', 'Cats', 'are', 'black', 'and', 'white,', 'Jellicle', 'Cats',   'are', 'rather', 'small;', 'Jellicle', 'Cats', 'are', 'merry', 'and',   'bright,', 'And', 'pleasant', 'to', 'hear', 'when', 'they', 'caterwaul.',   'Jellicle', 'Cats', 'have', 'cheerful', 'faces,', 'Jellicle', 'Cats',   'have', 'bright', 'black', 'eyes;', 'They', 'like', 'to', 'practise',   'their', 'airs', 'and', 'graces', 'And', 'wait', 'for', 'the', 'Jellicle',   'Moon', 'to', 'rise.', ''] 

I am trying to count how many times each word appears and display the top 3.

However I am only looking to find the top three that have the first letter capitalized and ignore all words that do not have the first letter capitalized.

I am sure there is a better way than this, but my idea was to do the following:

  1. put the first word in the list into another list called uniquewords
  2. delete the first word and all its duplicated from the original list
  3. add the new first word into unique words
  4. delete the first word and all its duplicated from original list.
  5. etc...
  6. until the original list is empty....
  7. count how many times each word in uniquewords appears in the original list
  8. find top 3 and print
like image 702
user434180 Avatar asked Aug 29 '10 11:08

user434180


People also ask

How do you find most repeated numbers in a list Python?

Make use of Python Counter which returns count of each element in the list. Thus, we simply find the most common element by using most_common() method.

How do you find common elements in a list?

Method 1:Using Set's & property Convert the lists to sets and then print set1&set2. set1&set2 returns the common elements set, where set1 is the list1 and set2 is the list2. Below is the Python3 implementation of the above approach: Python3.

How do you find common items between two lists?

Using sets Another approach to find, if two lists have common elements is to use sets. The sets have unordered collection of unique elements. So we convert the lists into sets and then create a new set by combining the given sets. If they have some common elements then the new set will not be empty.

How do I find the most common items in a list Python?

Use the most_common() of Counter to Find the Most Common Elements of a List in Python. In Python 2.7+, use the Counter() command to find the most common list elements in Python.


2 Answers

In Python 2.7 and above there is a class called Counter which can help you:

from collections import Counter words_to_count = (word for word in word_list if word[:1].isupper()) c = Counter(words_to_count) print c.most_common(3) 

Result:

[('Jellicle', 6), ('Cats', 5), ('And', 2)] 

I am quite new to programming so please try and do it in the most barebones fashion.

You could instead do this using a dictionary with the key being a word and the value being the count for that word. First iterate over the words adding them to the dictionary if they are not present, or else increasing the count for the word if it is present. Then to find the top three you can either use a simple O(n*log(n)) sorting algorithm and take the first three elements from the result, or you can use a O(n) algorithm that scans the list once remembering only the top three elements.

An important observation for beginners is that by using builtin classes that are designed for the purpose you can save yourself a lot of work and/or get better performance. It is good to be familiar with the standard library and the features it offers.

like image 162
Mark Byers Avatar answered Sep 21 '22 08:09

Mark Byers


If you are using an earlier version of Python or you have a very good reason to roll your own word counter (I'd like to hear it!), you could try the following approach using a dict.

Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29)  [GCC 4.2.1 (Apple Inc. build 5646)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> word_list = ['Jellicle', 'Cats', 'are', 'black', 'and', 'white,', 'Jellicle', 'Cats', 'are', 'rather', 'small;', 'Jellicle', 'Cats', 'are', 'merry', 'and', 'bright,', 'And', 'pleasant', 'to', 'hear', 'when', 'they', 'caterwaul.', 'Jellicle', 'Cats', 'have', 'cheerful', 'faces,', 'Jellicle', 'Cats', 'have', 'bright', 'black', 'eyes;', 'They', 'like', 'to', 'practise', 'their', 'airs', 'and', 'graces', 'And', 'wait', 'for', 'the', 'Jellicle', 'Moon', 'to', 'rise.', ''] >>> word_counter = {} >>> for word in word_list: ...     if word in word_counter: ...         word_counter[word] += 1 ...     else: ...         word_counter[word] = 1 ...  >>> popular_words = sorted(word_counter, key = word_counter.get, reverse = True) >>>  >>> top_3 = popular_words[:3] >>>  >>> top_3 ['Jellicle', 'Cats', 'and'] 

Top Tip: The interactive Python interpretor is your friend whenever you want to play with an algorithm like this. Just type it in and watch it go, inspecting elements along the way.

like image 44
Johnsyweb Avatar answered Sep 20 '22 08:09

Johnsyweb