| Store | Cart

Suggest more finesse, please. I/O and sequences.

From: Peter Hansen <pet...@engcorp.com>
Fri, 25 Mar 2005 21:09:41 -0500
Qertoip wrote:
> Good friend of mine heard about my attempt to create compact, simple Python> script and authored the following PHP script:
[snip 7-line PHP script]
> ...which has the same functionality with less actual code lines [7]. > I'm a little bit confused, since I considered Python more expressive then> PHP. The more I'm interested in improving my implementation now :)> It looks like this [11 actual code lines]:
[snip 11-line Python]

If you want fewer lines, it's not too hard.  Note that
if anyone has a problem with the style of this, I
point merely to the unreadable line noise that was
the PHP script as evidence that this is not about
style but conciseness:
--------------------------
import sys

corpus = {}
for word in open(sys.argv[1]).read().split():
     corpus[word] = corpus.get( word, 0 ) + 1

words = reversed(sorted(data[::-1] for data in corpus.iteritems()))

open(sys.argv[2], 'w').writelines('%7d : %s\n' % data for data in words)
--------------------------

I'm curious if either this or the PHP does what is really
wanted, however.  The above doesn't split on "words", but
merely on whitespace, making the results fairly meaningless
if you are concerned about punctuation etc.

-Peter

Recent Messages in this Thread
Qertoip Mar 25, 2005 06:17 pm
Scott David Daniels Mar 25, 2005 08:51 pm
Qertoip Mar 25, 2005 10:06 pm
Scott David Daniels Mar 25, 2005 11:30 pm
Larry Bates Mar 25, 2005 10:11 pm
Qertoip Mar 26, 2005 01:24 am
Peter Hansen Mar 26, 2005 02:09 am
Qertoip Mar 26, 2005 10:15 am
Peter Hansen Mar 26, 2005 11:46 am
Messages in this thread