This repository was archived by the owner on Mar 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathex14_2.py
More file actions
47 lines (38 loc) · 1.19 KB
/
ex14_2.py
File metadata and controls
47 lines (38 loc) · 1.19 KB
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
''' pickle is GREAT for this saving specified data structure task.
'''
import pickle
from collections import defaultdict
def load_words(filename = "words.txt"):
with open(filename) as f:
for word in f:
yield word.rstrip() # return a generator
def all_anagram(l):
'''Reads a list and return a set of anagram words
l: list
Returns: set
'''
d = defaultdict(list) # avoid KeyError in dict()
for word in l:
signature = "".join(sorted(word)) #sorted: leave the original word untouched
d[signature].append(word)
for k, v in d.items(): #remove d[k] if there is only one value corresponding to the key which means there is no anagram for the word
if len(v) == 1:
del d[k]
return d
def save_dict(d):
with open('shelf.pkl', 'wb') as f: #wb for write byte
pickle.dump(d, f, pickle.HIGHEST_PROTOCOL)
def look_up_dict(word):
#look up a word and return its anagram in the 'shelf'
with open('shelf.pkl', 'rb') as f: #rb for read byte
d = pickle.load(f)
for k, v in d.iteritems():
for i in v:
if i == word:
return v
if __name__ == '__main__':
books = all_anagram(load_words())
save_dict(books)
print ''
print look_up_dict('tired')
print look_up_dict('cosets')