-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex_09_dict_adv.py
44 lines (38 loc) · 1.37 KB
/
ex_09_dict_adv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#line = line.translate(line.maketrans('', '', string.punctuation))
'''To count some items from mail-box'''
def count():
find = 'from'
fname = input('Enter the file name: ')
try:
fhand = open(fname)
except:
print('File cannot be opened: ', fname)
exit()
dict_w = {}
for line in fhand:
line = line.rstrip()
line = line.lower()
if not line.startswith('{}'.format(find)): continue
#print(line) # from [email protected] thu jan 3 16:23:48 2008
words = line.split()
#print(words) #['from', '[email protected]', 'thu', 'jan', '3', '16:23:48', '2008']
if len(words) < 3: continue
dict_w[words[1]] = dict_w.get(words[1], 0) + 1
print(dict_w) # done!
##################################
fhand = open('words.txt')
counts = {}
for line in fhand:
#print(line) # Writing programs or programming is a very creative
words = line.split()
#print(words) # ['Writing', 'programs', 'or', 'programming', 'is', 'a', 'very', 'creative']
for word in words:
#print(word) # Writing
counts[word] = counts.get(word, 0) + 1 #''' The dictionary counts is done there'''
bigcount = None
bigword = None
for word,count in counts.items():
if bigcount is None or count > bigcount:
bigword = word
bigcount = count
print(bigword, bigcount)