Newbie with sort text file question
Max M
maxm at mxm.dk
Sun Jul 13 09:58:41 EDT 2003
stuartc wrote:
> 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)
> 2) Then summarized like this, ordered with the highest occurances
> first:
>
> orange occurs 4
> banana occurs 3
> apple occurs 2
>
> Total occurances is 9
fruity = """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"""
# print sorted list
fruits = fruity.split('\n')
fruits.sort()
print '\n'.join(fruits)
print ''
# count occurences
counter = {}
for fruit in fruits:
sort_of, apendix = fruit.split('_')
counter[sort_of] = counter.get(sort_of, 0) + 1
# sort by occurences
decorated = [(counter[key], key) for key in counter.keys()]
decorated.sort()
decorated.reverse()
# print result
sum = 0
for count, sort_of in decorated:
print sort_of, 'occurs', count
sum += count
print ''
print 'Total occurances is', sum
regards Max M
More information about the Python-list
mailing list