

If you need the labels - the Counter outperforms the inefficient process of wrapping the list in a Numpy array and then counting. The count() method is extremely fast compared to the other variants, however, it doesn't give us the labels associated with the counts like the other two do. Print( "Counter: %ss" % timeit.Timer(counterObject(words)).timeit( 1000)) Print( "count(): %ss" % timeit.Timer(countFunction(words)).timeit( 1000)) Print( "Pandas/Numpy: %ss" % timeit.Timer(pdNumpy(words)).timeit( 1000)) The task can be broken down into finding occurrences for all words or a single word, and we'll be doing benchmarks for both, starting with all words: import numpy as npĭef pdNumpy( words): def _pdNumpy(): return pd.value_counts(np.array(words))ĭef countFunction( words): def _countFunction():ĭef counterObject( words): def _counterObject(): return collections.Counter(words) Let's benchmark all of these to see how they perform. Naturally - you'll be searching for the most efficient solution if you're dealing with large corpora of words.

HOW TO GET WORD COUNT IN WORD CODE
If you run this code you should see this output: "hello" appears 3 time(s) The code below uses count() to get the number of occurrences for a word in a list: words = The complexity of the count() function is O(n), where n is the number of factors present in the list. The count() method is a built-in function that takes an element as its only argument and returns the number of times that element appears in the list. The "standard" way (no external libraries) to get the count of word occurrences in a list is by using the list object's count() function. This results in: Index: Index(, dtype='object') You can access its values field to get the counts themselves, or index to get the words themselves: df = pd.value_counts(np.array(words)) This results in a DataFrame that contains: hello 3 We can wrap the list into a Numpy array, and then call the value_counts() method of the pd instance (which is also available for all DataFrame instances): import numpy as np The shortest and easiest way to get value counts in an easily-manipulable format ( DataFrame) is via Numpy and Pandas. In practice, you'll use Pandas/Nunpy, the count() function or a Counter as they're pretty convenient to use. This guide will show you three different ways to count the number of word occurrences in a Python list: Say we have a list - we have two occurrences on "b" and one of "a".

Counting the word frequency in a list element in Python is a relatively common task - especially when creating distribution data for histograms.
