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

Qertoip qer1 at
Sat Mar 26 02:24:05 CET 2005

Dnia Fri, 25 Mar 2005 19:17:30 +0100, Qertoip napisał(a):

> Would you like to suggest me any improvements for the following code?
> I want to make my implementation as simple, as Python - native, as fine as
> possible.

> I've written simple code, which reads input text file and creates words' 
> ranking by number of appearence.

Good friend of mine heard about my attempt to create compact, simple Python
script and authored the following PHP script:

$data=join(' ', file($argv[1]));
foreach(explode(' ', $data) as $slowo)

array_multisort($stat, SORT_DESC, array_keys($stat));

foreach($stat as $sl=>$il) 
    $odata.="$il : $sl\n";

file_put_contents($argv[2], $odata);

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

import sys

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

words = sorted( ( -freq, word ) for word, freq in corpus.iteritems() )

outFile = open( sys.argv[2], 'w')
for negFreq, word in words:
    outFile.write( '%7d : %s\n' % ( -negFreq, word ) )

PS Thx 2 Scott David Daniels and Lary Bates


More information about the Python-list mailing list