Dont use variable name as sum
and file
,these word are use by python.
So my_file is ok because it not used by python an give a NameError.
>>> sum
<built-in function sum>
>>> file
<type 'file'>
>>> my_file
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
NameError: name 'my_file' is not defined
count()
as used over is ok to use for a word as "Olympics".
But to count other word,it can act not as expected.
If i want to count word "hi" that occurred 1 time in variable s i get 4.
>>> s = 'hi is hirohito in japan,higher or lower'
>>> s.count('hi')
4
There are several way to solve this,split(
) and using collections.Counter
can be one nice way.
>>> from collections import Counter
>>> s = 'hi is hirohito in japan,higher or lower'
>>> s = s.split()
>>> c = Counter(s)
>>> c['hi']
1