-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dicts.py
90 lines (73 loc) · 1.53 KB
/
Dicts.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
purse = dict()
purse['money'] = 12
purse['candy'] = 3
purse['tissues']=75
print(purse)
purse['candy'] = purse['candy'] + 2
#####
ccc = dict()
ccc['csev'] = 1
ccc['cwen'] = 1
print(ccc)
ccc['cwen']= ccc['cwen'] + 1
print(ccc)
#####
ccc = dict()
print(ccc['csev'])
#####
counts = dict()
names = ['csev', 'cwen', 'csev', 'zquian', 'cwen']
for name in names :
if name not in counts:
counts[name] = 1
else:
counts[name] = counts[name] + 1
print(counts)
#####
if name in counts:
x = counts[name]
else:
x = 0
x = counts.get(name, 0)
#####
counts = dict()
names = ['csev', 'cwen', 'csev', 'zqian', 'cwen']
for name in names:
counts[name] = counts.get(name, 0) + 1
print(counts)
#####
counts = dict()
print('Enter a line of text: ')
line = input('')
words = line.split()
print('Words', words)
for word in words:
counts[word] = counts.get(word, 0) + 1
print('Counts', counts)
#####
counts = {'chuck': 1, 'fred': 42, 'jan': 100}
for key in counts:
print(key, counts[key])
jjj = {'chuck':1, 'fred':42, 'jan':100}
print(list(jjj))
print(jjj.keys())
print(jjj.values())
print(jjj.items())
#####
for key, value in jjj.items():
print(key, value)
#####
name = input('Enter file: ')
handle = open(name)
counts = dict()
for line in handle:
words = line.split()
for word in words:
counts[word] = counts.get(word, 0) + 1
bigcount = None
bigword = None
for word, count in counts.items():
if bigcount is None or count > bigcount:
bigword = word
bigcount = count
print(bigword, bigcount)