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
The ideal way is to use a dictionary that maps a word to it's count. But if you can't use that, you might want to use 2 lists - 1 storing the words, and the other one storing counts of words. Note that order of words and counts matters here. Implementing this would be hard and not very efficient.
Using Counter would be the best way, but if you don't want to do that, you can implement it yourself this way.
freq will end up with the frequency of each word in the list you already have.
You need
float
in there to convert one of the integers to a float, so the resulting value will be a float.Edit:
If you can't use a dict or set, here is another less efficient way:
The indicies of
unique_words
andword_frequencies
will match.use this
Here is code support your question is_char() check for validate string count those strings alone, Hashmap is dictionary in python
Try this: