| Store | Cart

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

From: Qertoip <q...@o2.pl>
Sat, 26 Mar 2005 02:24:05 +0100
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)
    $stat[chop($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 inFile.read().split():
    corpus[word] = corpus.get( word, 0 ) + 1
inFile.close()

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 ) )
outFile.close()
--------------------------------------------------------------------------

PS Thx 2 Scott David Daniels and Lary Bates


-- 
Regards,
Piotrek

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