<br><br><div class="gmail_quote">On Thu, Jun 12, 2008 at 9:48 AM, Mark <<a href="mailto:markjturner@gmail.com">markjturner@gmail.com</a>> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi all,<br>
<br>
I have a scenario where I have a list like this:<br>
<br>
User            Score<br>
1                 0<br>
1                 1<br>
1                 5<br>
2                 3<br>
2                 1<br>
3                 2<br>
4                 3<br>
4                 3<br>
4                 2<br>
<br>
And I need to add up the score for each user to get something like<br>
this:<br>
<br>
User            Score<br>
1                 6<br>
2                 4<br>
3                 2<br>
4                 8<br>
<br>
Is this possible? If so, how can I do it? I've tried looping through<br>
the arrays and not had much luck so far.<br>
<br>
Any help much appreciated,<br>
</blockquote><div><br>If your data is set up as a list of tuples or lists, you can try using a dictionary for the final count, since each user will have only one final entry. Then, you can check to see if the user already has an entry in the dict so you know if you should create a new entry or just add to the old one.<br>
<br>firstList = [your pairs of data]<br>totals = {}<br>for i, j in firstList :<br>   if i in totals :<br>      totals[i] += j<br>   else :<br>      totals[i] = j<br> <br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
Mark<br>
<font color="#888888">--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</font></blockquote></div><br>