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: - if ID is not None:
-
if ID not in scoreDict:
-
scoreDict[ID] = set()
-
scoreDict[ID].add(Scores)
-
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
4 11105
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. - if ID not in scoreDict:
-
scoreDict[ID] = 0
-
scoreDict[ID] += score
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: -
scoreDict.setdefault(ID, 0)
-
scoreDict[ID] += int(score)
thanks a lot again,
Atrisa
Because score probably isn't defined. From your original code, it looks like you have a variable called scores but not score.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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,...
|
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...
|
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...
|
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,...
|
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...
|
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)
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
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....
| | |