defaultdict를 사용하면 코드를 단순화하고 이 문제를 극복할 수 있다.
def all_anagrams(filename):
d = defaultdict(list)
for line in open(filename):
word = line.strip().lower()
t = signature(word)
d[t].append(word)
return d
연습문제 18-3에 대한 해법은 http://thinkpython2.com/code/PokerHandSoln.py에서 받을 수 있다.
웹 사이트(http://thinkpython2.com/code/PokerHandSoln.py)에서 받을 수 있는 연습문제 18-3의 해법은 has_straightflush 함수에서 setdefault를 사용한다. 이 해법은 Hand 객체가 필요한가에 관계없이 루프를 반복할 때마다 Hand 객체를 생성하는 단점이 있다. 연습삼아 defaultdict를 사용해 이 함수를 재작성해보라.