I have been practicing python for the first time and I encountered this doubt. With a variable text, I inputed a small paragraph and split it based on spaces. So now I have words of that paragraph, but this is stored in a dictionary. Next I went on to find the number occurences of each word in the paragraph. My ultimate motive is to make a new list of words that appear more than 'x' number of times.
My code is:
text = '''Population refers to the number of individuals in a particular
place. It could be the number of humans or any other life form living in a
certain specified area. The number of tigers living in a forest is
therefore referred to as the population of tigers in the forest. The
number of people living in a town or city or an entire country is the
human population in that particular area.'''
words = text.split(" ")
a = dict()
for word in words:
if word not in a:
a[word] = 1
else:
a[word]+= 1
newlist = list()
val = 7
for key,value in a.items():
if a[key]>val:
newlist.append(i)
The final output that I receive after executing the last line is:
['years.', 'years.', 'years.', 'years.']
I don't know where I am going wrong