Hi All,
I have the following dictionary: -
{'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: -
{'1': [20], '3': [20], '2': [20], '4': [14]}
I have tried: - for item in f.values():
-
for items in item:
-
sum(items)
I am pretty sure I am way off here any advice where to start.
2 14789 bvdet 2,851
Recognized Expert Moderator Specialist
Hi All,
I have the following dictionary: -
{'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: -
{'1': [20], '3': [20], '2': [20], '4': [14]}
I have tried: - for item in f.values():
-
for items in item:
-
sum(items)
I am pretty sure I am way off here any advice where to start.
This is one way: - >>> dd = {'1': [10, 5, 4, 1], '3': [10, 5, 4, 1], '2': [10, 5, 4, 1], '4': [10, 4]}
-
>>> dd2 = dict(zip(dd.keys(), [[sum(item)] for item in dd.values()]))
-
>>> dd2
-
{'1': [20], '3': [20], '2': [20], '4': [14]}
-
>>>
another one: - >>> dd3 = {}
-
>>> for key in dd:
-
... dd3[key] = [sum(dd[key])]
-
...
-
>>> dd3
-
{'1': [20], '3': [20], '2': [20], '4': [14]}
-
>>>
You can also update the original: - >>> for key in dd:
-
... dd[key] = [sum(dd[key])]
-
...
-
>>> dd
-
{'1': [20], '3': [20], '2': [20], '4': [14]}
-
>>>
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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...
|
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: 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...
|
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...
| |
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: 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...
|
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,...
|
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...
|
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...
|
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...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |