In [11]:
worddict = dict()
count = 0
fname = input("Which File?")
search = input('Search for what word?')
fopen = open(fname)
read = fopen.read().split()
for word in read :
if word.lower() not in worddict :
worddict[word.lower()] = 1
else :
worddict[word.lower()] = worddict[word.lower()] + 1
print(worddict)
search in read
print (worddict.get('and', 0))
{'but': 1, 'soft': 1, 'what': 1, 'light': 1, 'through': 1, 'yonder': 1, 'window': 1, 'breaks': 1, 'it': 1, 'is': 3, 'the': 3, 'east': 1, 'and': 3, 'juliet': 1, 'sun': 2, 'arise': 1, 'fair': 1, 'kill': 1, 'envious': 1, 'moon': 1, 'who': 1, 'already': 1, 'sick': 1, 'pale': 1, 'with': 1, 'grief': 1} 3
In [14]:
worddict = dict()
fname = input ('Which File?')
fopen = open(fname)
read = fopen.read().split()
for word in read :
worddict[word] = worddict.get(word, 0) +1
print(worddict)
{'But': 1, 'soft': 1, 'what': 1, 'light': 1, 'through': 1, 'yonder': 1, 'window': 1, 'breaks': 1, 'It': 1, 'is': 3, 'the': 3, 'east': 1, 'and': 3, 'Juliet': 1, 'sun': 2, 'Arise': 1, 'fair': 1, 'kill': 1, 'envious': 1, 'moon': 1, 'Who': 1, 'already': 1, 'sick': 1, 'pale': 1, 'with': 1, 'grief': 1}
In [45]:
worddict = dict()
bigcount = None
fopen = open('mbox-short.txt')
for line in fopen :
line = line.rstrip()
if line.startswith('From:') :
sline = line.split(' ')
worddict[sline[1]] = worddict.get(sline[1], 0) + 1
for key in worddict :
if bigcount is None or worddict[key] > bigcount :
bigcount = worddict[key]
for key in worddict :
if worddict[key] == bigcount :
print(key, worddict[key])
In [ ]: