Mercurial > slides-fragments
changeset 172:97691ea64200
created
| author | 127.0.0.1 |
|---|---|
| date | Thu, 30 Jan 2014 10:08:32 +0100 |
| parents | ce8f7cbc1fb5 |
| children | da0365e71739 |
| files | counter |
| diffstat | 1 files changed, 22 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/counter Thu Jan 30 10:08:32 2014 +0100 @@ -0,0 +1,22 @@ +**Fine:** +{{{#!python +score_list = [ + ('alice', 5), + ('bob', 1), + ('bob', 3), + ('alice', 2), + ... +] +total_scores_by_name = {} +for name, score in score_list: + total_scores_by_name[name] = total_scores_by_name.get(name, 0) + score +}}} + +**Better:** +{{{#!python +import collections + +total_scores_by_name = collectins.Counter() +for name, score in score_list: + total_scores_by_name[name] += score +}}} \ No newline at end of file
