Newbie with sort text file question

stuartc stuart_clemons at us.ibm.com
Sat Jul 12 15:46:51 EDT 2003


Hi:

I'm not a total newbie, but I'm pretty green.  I need to sort a text
file and then get a total for the number of occurances for a part of
the string. Hopefully, this will explain it better:

Here's the text file: 

banana_c \\yellow
apple_a \\green
orange_b \\yellow
banana_d \\green
orange_a \\orange
apple_w \\yellow
banana_e \\green
orange_x \\yellow
orange_y \\orange

I would like two output files:

1) Sorted like this, by the fruit name (the name before the dash)

apple_a \\green
apple_w \\yellow
banana_c \\yellow
banana_d \\green
banana_e \\green
orange_a \\orange
orange_b \\yellow
orange_x \\yellow
orange_y \\orange

2) Then summarized like this, ordered with the highest occurances
first:

orange occurs 4
banana occurs 3
apple occurs 2

Total occurances is 9

Thanks for any help !




More information about the Python-list mailing list