473,770 Members | 4,544 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

In a dictionary, sum values if similar keys

22 New Member
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 11139
dwblas
626 Recognized Expert Contributor
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 New Member
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 Recognized Expert Moderator MVP
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 Recognized Expert Contributor
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
8735
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, "New file(s)!!!" My key-values pairs are filepaths and their modify times. I want to
11
6083
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 dicts with larger no. of features. Now i want to check each pair to see if they are connected...element of this pair will be one from the first list and one from the second....e.g for 'ab' i want to check if 1 and 3 are connected,then 1 and...
9
3192
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 any probems with this... here is an example: +++++++++++++++++++++++++++++++++++++++
6
15487
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, stringcollection = new Dictionary<long, string>(); string myString = "Hello World";
5
1967
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 it would assume the insignificance that it deserves in the grand scheme of my program. I had in mind something like this:
10
1876
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
910
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 4, respectively print NT which gives:
1
1447
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 hint. (if you asked me for a hint, I'd tell you to look for questions about dictionaries in the mailing list archives).
10
6559
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 distinct values ?
14
3818
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 in the internal list of phno of that dictionary? thanks you.
0
9425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10225
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10053
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9867
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7415
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6676
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5312
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3573
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.