-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdictionaries.py
More file actions
26 lines (25 loc) · 884 Bytes
/
dictionaries.py
File metadata and controls
26 lines (25 loc) · 884 Bytes
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
dictionaries = {'a': 1, 'b': 2, 'c': 3}
print(len(dictionaries)) # 3
#print(dictionaries.clear()) # empty dict
print(dictionaries.copy) # <built-in method copy of dict object at 0x7fe08e147c40>
print(sorted(list(dictionaries))) # ['a', 'b', 'c']
print(sorted(list(dictionaries.values()))) # [1, 2, 3]
print(sorted(list(dictionaries), key=dictionaries.__getitem__)) # ['a', 'b', 'c']
print(sorted(list(dictionaries),reverse=True))
print([value for (key, value) in sorted(dictionaries.items())]) # [1, 2, 3]
def wordcount(filename):
try:
data = open(filename)
except:
print("Can't reach the file")
exit()
count = dict()
for line in data:
words = line.split()
for word in words:
if word not in count:
count[word] = 1
else:
count[word] += 1
return count
wordcount()