3 ms·
On the topic of "UNIX shell one-liners versus Python", I enjoy letting my students solve the "make a list of the words in a file with their counts" exercise in
by jll29 3y ago
On the topic of "UNIX shell one-liners versus Python", I enjoy letting my students solve the "make a list of the words in a file with their counts" exercise in Python, then I show them a sh one-liner.
The Python scripts takes them from 10 minutes to two hours, they're buggy, and they cannot process big files - everything is in RAM (with a monster dict) - without a crash/system freeze.
The power of UNIX, pipes and re-use.
- d0mine 3y agoSpace-separated case: import sys from collections import Counter L = Counter( word for line in sys.stdin for word in line.casefold().split() ) L is the words' multiset (the desired list of words with counts). How many words are there in English? 1M? Huge text [Unicode] files. Fancier tokenizer: for word in re.findall(r"\w+", line.casefold())