開発環境
- OS X El Capitan - Apple (OS)
- Emacs (Text Editor)
- Python 3.5 (プログラミング言語)
Think Python (Allen B. Downey (著)、 O'Reilly Media)のChapter 14.(Files)のExercises 14-2.(No. 3375)を取り組んでみる。
Exercises 14-2.(No. 3375)
コード(Emacs)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import anagram_sets
import shelve
def store_anagrams(anagrams):
with shelve.open('anagrams') as shelf:
for k, v in anagrams.items():
shelf[k] = v
def read_anagrams(word):
with shelve.open('anagrams') as shelf:
res = shelf.get(word)
return res
if __name__ == '__main__':
filename = 'words.txt'
anagrams = anagram_sets.all_anagrams(filename)
store_anagrams(anagrams)
print(read_anagrams('opst'))
入出力結果(Terminal, IPython)
$ ./sample2.py ['opts', 'post', 'pots', 'spot', 'stop', 'tops'] $
0 コメント:
コメントを投稿