Count frequency of words in a list and sort by frequency

Viewed 264906

I am using Python 3.3

I need to create two lists, one for the unique words and the other for the frequencies of the word.

I have to sort the unique word list based on the frequencies list so that the word with the highest frequency is first in the list.

I have the design in text but am uncertain how to implement it in Python.

The methods I have found so far use either Counter or dictionaries which we have not learned. I have already created the list from the file containing all the words but do not know how to find the frequency of each word in the list. I know I will need a loop to do this but cannot figure it out.

Here's the basic design:

 original list = ["the", "car",....]
 newlst = []
 frequency = []
 for word in the original list
       if word not in newlst:
           newlst.append(word)
           set frequency = 1
       else
           increase the frequency
 sort newlst based on frequency list 
15 Answers

Pandas answer:

import pandas as pd
original_list = ["the", "car", "is", "red", "red", "red", "yes", "it", "is", "is", "is"]
pd.Series(original_list).value_counts()

If you wanted it in ascending order instead, it is as simple as:

pd.Series(original_list).value_counts().sort_values(ascending=True)

Try this:

words = []
freqs = []

for line in sorted(original list): #takes all the lines in a text and sorts them
    line = line.rstrip() #strips them of their spaces
    if line not in words: #checks to see if line is in words
        words.append(line) #if not it adds it to the end words
        freqs.append(1) #and adds 1 to the end of freqs
    else:
        index = words.index(line) #if it is it will find where in words
        freqs[index] += 1 #and use the to change add 1 to the matching index in freqs

Here is code support your question is_char() check for validate string count those strings alone, Hashmap is dictionary in python

def is_word(word):
   cnt =0
   for c in word:

      if 'a' <= c <='z' or 'A' <= c <= 'Z' or '0' <= c <= '9' or c == '$':
          cnt +=1
   if cnt==len(word):
      return True
  return False

def words_freq(s):
  d={}
  for i in s.split():
    if is_word(i):
        if i in d:
            d[i] +=1
        else:
            d[i] = 1
   return d

 print(words_freq('the the sky$ is blue not green'))
for word in original_list:
   words_dict[word] = words_dict.get(word,0) + 1

sorted_dt = {key: value for key, value in sorted(words_dict.items(), key=lambda item: item[1], reverse=True)}

keys = list(sorted_dt.keys())
values = list(sorted_dt.values())
print(keys)
print(values)

Simple way

d = {}
l = ['Hi','Hello','Hey','Hello']
for a in l:
    d[a] = l.count(a)
print(d)
Output : {'Hi': 1, 'Hello': 2, 'Hey': 1} 

word and frequency if you need

def counter_(input_list_):
  lu = []
  for v in input_list_:
    ele = (v, lc.count(v)/len(lc)) #if you don't % remove <</len(lc)>>
    if ele not in lu:
      lu.append(ele)
  return lu

counter_(['a', 'n', 'f', 'a'])

output:

[('a', 0.5), ('n', 0.25), ('f', 0.25)]
Related