I am trying to list the top 3 most occurring words from a text document. I
have managed to distill it down to the top three [13, 22, 24]. But for
some reason my final print statement gives me the 4 most reoccurring words
and not even in a numerical order [22, 22, 24, 13 ] Could someone show me
why this is happening ?
I have attached the text file that I am sourcing called EinsteinCredo.txt
''' Read this text file and return the top three most ocurring words '''
inFile = r'E:\ProfessionalDevelopment\python\Introduction to Python
Scripting in Maya\week4\EinsteinCredo.txt'
wordList=[]
occurences=[]
with open(inFile, 'r') as fin:
# removes the punctuation and splits the words into a list
for line in fin:
punct = ["'","?",".","!","?",",","\r\n","-"]
for p in punct:
line = line.replace(p,"").upper()
line = line.split()
for word in line:
wordList.append(word)
# make a word count list
for x in wordList:
occurences.append(wordList.count(x))
# make a dictionary of both the wordList and occurences
wordFrequencey = dict(zip(wordList,occurences))
# find the top three most occuring words
order = list(set(sorted(wordFrequencey.values())))
topThree = order[-3:]
# print the results
for k, v in wordFrequencey.items():
if v in topThree:
print 'the word " %s " occured %s times' % (k,v)
--
You received this message because you are subscribed to the Google Groups
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/python_inside_maya/0655c287-af16-4657-a808-e77d56d65ed3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
My Credo
by Albert Einstein
Our situation on this earth seems strange. Every one of us appears here
involuntarily
and uninvited for a short stay, without knowing the whys and the wherefore. In
our
daily lives we only feel that man is here for the sake of others, for those
whom we
love and for many other beings whose fate is connected with our own.
I am often worried at the thought that my life is based to such a large extent
on the work of my fellow human beings and I am aware of my great indebtedness
to them.
I do not believe in freedom of the will. Schopenhauer's words: "Man can do what
he
wants, but he cannot will what he wills" accompany me in all situations
throughout
my life and reconcile me with the actions of others even if they are rather
painful
to me. This awareness of the lack of freedom of will preserves me from taking
too
seriously myself and my fellow men as acting and deciding individuals and from
losing my temper.
I never coveted affluence and luxury and even despise them a good deal.
My passion for social justice has often brought me into conflict with people,
as
did my aversion to any obligation and dependence I do not regard as absolutely
necessary. I always have a high regard for the individual and have an
insuperable
distaste for violence and clubmanship.
All these motives made me into a passionate pacifist and anti-militarist. I am
against any nationalism, even in the guise of mere patriotism. Privileges based
on position and property have always seemed to me unjust and pernicious, as did
any exaggerated personality cult.
I am an adherent of the ideal of democracy, although I well know the weaknesses
of the democratic form of government. Social equality and economic protection
of
the individual appeared to me always as the important communal aims of the
state.
Although I am a typical loner in daily life, my consciousness of belonging to
the
invisible community of those who strive for truth, beauty, and justice has
preserved
me from feeling isolated.
The most beautiful and deepest experience a man can have is the sense of the
mysterious.
It is the underlying principle of religion as well as all serious endeavour in
art and science. He who never had this experience seems to me, if not dead, then
at least blind. To sense that behind anything that can be experienced there is
a something that our mind cannot grasp and whose beauty and sublimity reaches us
only indirectly and as a feeble reflection, this is religiousness.
In this sense I am religious. To me it suffices to wonder at these secrets and
to
attempt humbly to grasp with my mind a mere image of the lofty structure of all
that there is.