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

2005-03-26 Thread Peter Hansen
Qertoip wrote: Peter Hansen wrote: -- 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 f

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

2005-03-26 Thread Qertoip
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 functionalit

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

2005-03-25 Thread Peter Hansen
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 expressiv

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

2005-03-25 Thread Qertoip
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' >

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

2005-03-25 Thread Scott David Daniels
Qertoip wrote: Dnia Fri, 25 Mar 2005 12:51:59 -0800, Scott David Daniels napisał(a): > ... for word in line.split(): try: corpus[word] += 1 except KeyError: corpus[word] = 1 Above is (probably) not efficient when exception is thrown, t

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

2005-03-25 Thread Larry Bates
You might take advantage of the .get method on dictionaries to rewrite: wordsDic = {} inFile = open( sys.argv[1] ) for word in inFile.read().split(): if wordsDic.has_key( word ): wordsDic[word] = wordsDic[word] + 1 else: wordsDic[word] = 1 as: wordsDic = {} inFile = open(

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

2005-03-25 Thread Qertoip
Dnia Fri, 25 Mar 2005 12:51:59 -0800, Scott David Daniels napisał(a): Thanks for your reply! It was really enlightening. > How about: > for line in inFile: > for word in line.split(): > try: > corpus[word] += 1 > except KeyError: >

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

2005-03-25 Thread Scott David Daniels
Qertoip wrote: import sys def moreCommonWord( x, y ): if x[1] != y[1]: return cmp( x[1], y[1] ) * -1 return cmp( x[0], y[0] ) If you want to keep this, use: def moreCommonWord(x, y): if x[1] != y[1]: return cmp(y[1], x[1]) return cmp(x

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

2005-03-25 Thread Qertoip
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. Code: --