Re:Homogenization of scientific computing – Python is eating...
by drkevorkian on Thursday January 09, 2014 @03:24PM
(#7032934)
[ Parent
| on HN ]
Re:Homogenization of scientific computing – Python is eating...
by Goladus on Thursday January 09, 2014 @08:05PM
(#7034392)
I like this one. Though I would do it like this to keep the lines under 80 characters:
words=open('test.txt').read().lower().split()
for word, count in Counter(words).most_common():
print word, count
(edited per child comment)
Re:Homogenization of scientific computing – Python is eating...
by benhoyt on Thursday January 09, 2014 @09:14PM
(#7034663)
Yeah, those are nice -- and may actually be more efficient on smaller files, as you're only doing the lower() once on a big string. However, for big files you don't necessarily want to read the whole thing in at once.
One nitpick: it's Pythonic (I think) to just name the list of words "words" rather than "word_list".
Re:Homogenization of scientific computing – Python is eating...
by Goladus on Friday January 10, 2014 @09:29AM
(#7036694)
Yes that's a classic tradeoff, a proficient programmer will have to pick one.
Personally I always read entire files into memory first unless I have reason to believe memory will be an issue or need to program defensively against malicious/careless input. The code is always much cleaner and easier to read and if you need to do a second pass on the data you don't need to re-read it from disk.