Efficiently count word frequencies in python
The most succinct approach is to use the tools Python gives you. from future_builtins import map # Only on Python 2 from collections import Counter from itertools import chain def countInFile(filename): with open(filename) as f: return Counter(chain.from_iterable(map(str.split, f))) That’s it. map(str.split, f) is making a generator that returns lists of words from each line. Wrapping … Read more