473,237 Members | 1,399 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,237 software developers and data experts.

Sum values stored in a dictionary as a list of values.

GTXY20
29
Hi All,

I have the following dictionary:

Expand|Select|Wrap|Line Numbers
  1. {'1': [10, 5, 4, 1], '3': [10, 5, 4, 1], '2': [10, 5, 4, 1], '4': [10, 4]}
I need to sum the values for each key and make the dictionary:

Expand|Select|Wrap|Line Numbers
  1. {'1': [20], '3': [20], '2': [20], '4': [14]}
I have tried:


Expand|Select|Wrap|Line Numbers
  1. for item in f.values():
  2.                 for items in item:
  3.                     sum(items)
I am pretty sure I am way off here any advice where to start.
Oct 5 '07 #1
2 14699
bvdet
2,851 Expert Mod 2GB
Hi All,

I have the following dictionary:

Expand|Select|Wrap|Line Numbers
  1. {'1': [10, 5, 4, 1], '3': [10, 5, 4, 1], '2': [10, 5, 4, 1], '4': [10, 4]}
I need to sum the values for each key and make the dictionary:

Expand|Select|Wrap|Line Numbers
  1. {'1': [20], '3': [20], '2': [20], '4': [14]}
I have tried:


Expand|Select|Wrap|Line Numbers
  1. for item in f.values():
  2.                 for items in item:
  3.                     sum(items)
I am pretty sure I am way off here any advice where to start.
This is one way:
Expand|Select|Wrap|Line Numbers
  1. >>> dd = {'1': [10, 5, 4, 1], '3': [10, 5, 4, 1], '2': [10, 5, 4, 1], '4': [10, 4]}
  2. >>> dd2 = dict(zip(dd.keys(), [[sum(item)] for item in dd.values()]))
  3. >>> dd2
  4. {'1': [20], '3': [20], '2': [20], '4': [14]}
  5. >>> 
another one:
Expand|Select|Wrap|Line Numbers
  1. >>> dd3 = {}
  2. >>> for key in dd:
  3. ...     dd3[key] = [sum(dd[key])]
  4. ...     
  5. >>> dd3
  6. {'1': [20], '3': [20], '2': [20], '4': [14]}
  7. >>> 
You can also update the original:
Expand|Select|Wrap|Line Numbers
  1. >>> for key in dd:
  2. ...     dd[key] = [sum(dd[key])]
  3. ...     
  4. >>> dd
  5. {'1': [20], '3': [20], '2': [20], '4': [14]}
  6. >>> 
Oct 5 '07 #2
GTXY20
29
Perfect!

Thanks so much.
Oct 5 '07 #3

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

Similar topics

5
by: Rakesh | last post by:
Hi, For a particular problem of mine, I want to sort <key, value> pairs by its value. Eg: Input: A, 4 B, 5
17
by: Roland Hall | last post by:
Is there a way to return multiple values from a function without using an array? Would a dictionary object work better? -- Roland Hall /* This information is distributed in the hope that it...
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...
14
by: vatamane | last post by:
This has been bothering me for a while. Just want to find out if it just me or perhaps others have thought of this too: Why shouldn't the keyset of a dictionary be represented as a set instead of a...
4
by: O.B. | last post by:
I need the ability to parse through the values of a Dictionary and remove certain ones depending on their attribute values. In the example below, an InvalidOperationException is thrown in the...
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...
13
by: Nader | last post by:
Hello, I have a dictionary and will get all keys which have the same values. d = {('a' : 1), ('b' : 3), ('c' : 2),('d' : 3),('e' : 1),('f' : 4)} I will something as : d.keys(where their...
10
by: ++imanshu | last post by:
Hi, Wouldn't it be nicer to have 'in' return values (or keys) for both arrays and dictionaries. Arrays and Dictionaries looked so similar in Python until I learned this difference. Thanks,...
0
by: Maric Michaud | last post by:
Le Thursday 28 August 2008 03:43:16 norseman, vous avez écrit : Disctionaries are hash tables with a unique key and constant time lookup. What you want could be implemented as a complex data...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.