On 26/10/10 21:20, masawudu bature wrote:
How do I sum all occurrences of  a number in a file?
I have a file with a bunch of numbers, each on it's own line:
5
3
11
3
7
3
5
5
11
7
7
...
How do i sum them up so that the output will be ,
5 :  15
3 :   9
11:  22
7 :   21

Assuming you know how to iterate through a file, you could try saving the number of occurrences of each number in the file into a dictionary.
Something like:

if num in my_dict:
    my_dict[num] += 1
else:
    my_dict[num] = 1

HTH.
Adam.
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to