3 ms·
Or, use a defaultdict(int), or more in line with yours: d[word] = d.get(word,0) + 1 dictionary.get is quite useful. Also, I'd consider s.split(None), instead
by wiredfool 13y ago
Or, use a defaultdict(int), or more in line with yours:
d[word] = d.get(word,0) + 1
dictionary.get is quite useful.
Also, I'd consider s.split(None), instead of s.split(' '). It will group whitespace, so that any double space or other whitespace is collapsed into one delimiter.
- shill 13y agoI didn't know about split(None). Thanks!
- bereft_orange 13y agoor just `s.split()`