Solve word-count problem in Python