by flubert · 6y ago · 0 comments
from collections import defaultdict
...: d=defaultdict(int)
...: for fn in glob.iglob("**/*.txt"):
...: with open(fn, 'r') as f:
...: for line in f:
...: for word in line.split():
...: d[word] = d[word] + 1
...: [(k, v) for k, v in sorted(d.items(), key=lambda x: x[1], reverse=True)][:10]
That said, the C++ version in the article could be tightened up a bit. What's happening on line 34, for instance? find . -type f -name '*.txt' -print0
| xargs -0 cat | tr ' \t' '\n\n' | grep '\S'
| sort | uniq -c | sort -nr | sed 10q
I'm assuming that when it says find all the .txt files in subdirectories of the current directory, that should include .txt files in the current directory.