-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter1-mini-project
59 lines (48 loc) · 2.06 KB
/
chapter1-mini-project
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# a program to find the most common word in a text file
name = raw_input ('Enter file:') #C:\Users\Sharon\Desktop\Python\cmd.txt
handle = open (name, 'r')
text = handle.read()
words = text.split()
counts = dict() # define a function in dictonary, which will be called later
for word in words:
counts[word] = counts.get(word,0) +1
bigcount = None # None can be any type
bigword = None
for word, count in counts.items(): #items() is a pair
# print word,count
# check how items() read the file
if bigcount is None or count > bigcount: #output is the FIRST word hit the frequency
#if bigcount is None or count >= bigcount: #output is the LAST word hit the frequency
bigword = word
bigcount = count
# for loop doesn't need "else"
print bigword, bigcount
# 31 reserved keywords
# and del from not while as elif global or with assert else if pass yield break except import print
# class exec in raise continue finally is return def for lambda try
# functions:
# quit(), 42%10 ->2 (modulus operator, operand), x = int(98.6) ->98
# new worlds: mnemonic,Parenthesis,concatenate, floor division, integer, precedence, boolean
name = raw_input ('Enter file:') #C:\Users\Sharon\Desktop\Python\cmd.txt
handle = open (name, 'r')
text = handle.read()
words = text.split()
counts = dict() # define a dictionary called counts
# My bonus exercies: there are two words, both are used most
for word in words:
counts[word] = counts.get(word,0) +1
bigcount1 = None # None can be any type
bigword1 = None
bigword2 = None
for word, count in counts.items(): #items() is a pair
# print word,count
# check how items() read the file
if bigcount1 is None or count > bigcount1: #output is the FIRST word hit the frequency
#if bigcount is None or count >= bigcount: #output is the lAST word hit the frequency
bigword1 = word
bigcount1 = count
elif count == bigcount1: # == means equal, '=' is assigning value to a variable
bigword2 = word #indentation
# for loop doesn't need "else"
print bigword1, bigcount1
print bigword2, bigcount1