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

Qertoip qer1 at o2.pl
Sat Mar 26 05:15:53 EST 2005


Dnia Fri, 25 Mar 2005 21:09:41 -0500, Peter Hansen napisał(a):

Thanks for comments! :)

> 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]
 
> --------------------------
> 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)
> --------------------------

Is the file automatically closed in both cases?


> 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.

You are perfectly right, but the requirements were intentionally
simplified, ensuring 0-errors input and allowing words like 'hey,!1_go'

I aim to make my implementation as concise as possible but 
*steel being natural, readable and clear*.


-- 
Pozdr. 
Piotrek



More information about the Python-list mailing list