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
You can use
It supports Python 2.7,read more information here
1.
use dict
But, You have to read the file first, and converted to dict.
2. it's the python docs example,use re and Counter
One way would be to make a list of lists, with each sub-list in the new list containing a word and a count:
Or, more efficiently:
This would be less efficient than using a dictionary, but it uses more basic concepts.
You can use reduce() - A functional way.
returns:
the best thing to do is :
then try to :
wordListToFreqDict(originallist)
Yet another solution with another algorithm without using collections: