Newbie with sort text file question
Bob Gailer
bgailer at alum.rpi.edu
Mon Jul 14 09:10:39 EDT 2003
At 03:12 PM 7/13/2003 -0600, Andrew Dalke wrote:
>Bob Gailer:
> > [Pipeline]
>
>Huh. Hadn't heard of that one before. Thanks for the pointer.
Since I am developing the Python version of Pipeline I wonder if you have
any interest in it? Would like to be an early recipient?
>(And overall, nice post!)
>
> > The Python version:
>
>Some stylistic comments
>
> > input = file('c:\input.txt')
>
>Since 'input' is a builtin, I use 'infile'.
Agree. When I'm in a hurry I let details slip.
>For the OP, you'll need 'c:\\input.txt' because the '\' has special meaning
>inside of a string so must be escaped.
Agree. When I'm in a hurry I let details slip.
> > fruits = {} # a dictionary to hold each fruit and its count
> > lines = input.readlines()
> > for line in lines:
>
>Since you are using Python 2.2 (later you use "if fruit in fruits",
>and "__in__" support for dicts wasn't added until Python 2.2, I
>think, and the 'file' usage is also new), this is best written as
>
> for line in input:
Agree.
> > fruit = line.split('_', 1)[0]
>
> > if fruit in fruits:
> > fruits[fruit] += 1 # increment count
> > else:
> > fruits[fruit] = 1 # add to dictionary with count of 1
>
>Here's a handy idiom for what you want
>
> fruits[fruit] = fruits.get(fruit, 0) + 1
Don't you want setdefault() instead of get()?
> > output1 = file('c:\output1.txt', 'w')
> > for key, value in fruits.items():
> > output1.write("%s occurs %s\n" % (key, value))
> > output1.close()
> > output2 = file('c:\output2.txt', 'w')
> > output2.write("Total occurrences is %s\n" % len(lines))
> > output2.close()
>
>That's missing some sorts, so I don't think it meets the OP's requirements.
The only reason for sort that I could see was to group things for counting.
The output appears sorted descending, but that order was not specified, so
I assumed random output.
[snip]
Bob Gailer
bgailer at alum.rpi.edu
303 442 2625
-------------- next part --------------
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.500 / Virus Database: 298 - Release Date: 7/10/2003
More information about the Python-list
mailing list