473,320 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

In a dictionary, sum values if similar keys

22
Hi everyone. I have a file of records like this:

ID:13
Date: 12.12
Scores: 2

ID:22
Date: 9.12
Scores: 4

ID:13
Date: 18.12
Scores: 4

I want to have the unique IDs as keys and the sum of the socres for that ID as values in the scoreDict dictionary. This is my code so far:

Expand|Select|Wrap|Line Numbers
  1. if ID is not None:
  2.     if ID not in scoreDict:
  3.         scoreDict[ID] = set()
  4.     scoreDict[ID].add(Scores)
  5.     ID = None
I want to have for example for person with ID 13 a sum of all her goals. How do I do the summation inside that piece of code? Any guide or help is appreciated.

Best regards, Atrisa
Dec 21 '10 #1
4 11105
dwblas
626 Expert 512MB
All that is required is an integer that holds the total. Also, you do not need
if ID is not None:
as long as "None" is not in the dictionary.
Expand|Select|Wrap|Line Numbers
  1. if ID not in scoreDict:
  2.     scoreDict[ID] = 0
  3. scoreDict[ID] += score 
Dec 22 '10 #2
Atrisa
22
Thanks dwblas. For some reason it kept telling me that score is not defined, though it is. After making some changes, now the working code looks like this:
Expand|Select|Wrap|Line Numbers
  1. scoreDict.setdefault(ID, 0)
  2. scoreDict[ID] += int(score)
thanks a lot again,
Atrisa
Dec 22 '10 #3
Rabbit
12,516 Expert Mod 8TB
Because score probably isn't defined. From your original code, it looks like you have a variable called scores but not score.
Dec 22 '10 #4
dwblas
626 Expert 512MB
If you want to keep the individual scores in the dictionary in some future program, use a list instead of a set. A set does not allow duplicate entries, so if you have 2 scores that are the same for any key, only one will be in the set.
Dec 22 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: rbt | last post by:
What's a good way to compare values in dictionaries? I want to find values that have changed. I look for new keys by doing this: new = if new == : print new, "No new files." else: print new,...
11
by: Girish Sahani | last post by:
I wrote the following code to concatenate every 2 keys of a dictionary and their corresponding values. e.g if i have tiDict1 = tiDict1 = {'a':,'b':} i should get tiDict2={'ab':} and similarly for...
9
by: jojoba | last post by:
hello! i am trying to come up with a simple way to access my values in my nested python dictionaries here is what i have so far, but i wanted to run it by the geniuses out there who might see...
6
by: Martin | last post by:
Hi all, I'd like to find a .NET Collection in which I will be able to set many keys for the same object. For example, I would like to do something like that : Dictionary<long,...
5
by: j1o1h1n | last post by:
Hello, I was trying to create a flattened list of dictionary values where each value is a list, and I was hoping to do this in some neat functionally style, in some brief, throwaway line so that...
10
by: psbasha | last post by:
Hi, Dictionary works in getting values from the key values.But I would like to get keys from values. For example : d = {100: ,102: ,202: } d.get(102)
0
by: Brandon | last post by:
Hi all, I'm not sure if I'm calling the right method in a dictionary. I have: for k,v in dict.items(): NT = k,range(alpha,omega) #where alpha and omega are previously defined as 1 and...
1
by: Fredrik Lundh | last post by:
"Graps Graps" wrote: someone asked a very similar question not long ago, so maybe you're really supposed to figure this out yourself. I'm sure your teacher won't kill you if you ask for a...
10
by: aurekha | last post by:
Hi I have hashes with arrays to its keys like, %h1 = ('a'=>, 'b'=>, 'c'=> ); %h2 = ('a'=>, 'b'=>); then, how can i compare the 2 hashes(based on values. not keys) and get...
14
by: lee | last post by:
hi, i have a dictionary as follows : kev : {'phno': , 'email': , 'name': , 'address': } if user is enters the 3rd item of key phno, ie "dfsdf" in my dict, how can i find it is the third item...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.