Merge and sum of two dictionaries

Merge and sum of two dictionaries

Hi,

I want to merge the two dictionaries and at the same I want to sum the vales of the same key.

How I can achieve this?

Thanks

View Answers

July 18, 2021 at 5:28 PM

Hi,

this is very classical example of summing the marks obtained by a student in each terms:

from collections import Counter

term1 = {"physics":20,"chemistry":20,"math":25,"english":25}
term2 = {"physics":25,"chemistry":28,"math":35,"english":30}
term3 = {"physics":30,"chemistry":25,"math":29,"english":34}


sumAllTerms= Counter(term1) + Counter(term2) + Counter(term3)

print(sumAllTerms)

Thanks


July 18, 2021 at 5:28 PM

Hi,

Check more tutorials at:

Thanks

Ads









Related Tutorials/Questions & Answers:
Merge and sum of two dictionaries
Merge and sum of two dictionaries  Hi, I want to merge the two dictionaries and at the same I want to sum the vales of the same key. How I can achieve this? Thanks   Hi, this is very classical example of summing
python merge two dictionaries add values
python merge two dictionaries add values  Hi, In Python I have two dictionaries with same keys. I want to merge the two dictionaries and add...,"chemistry":25,"math":29,"english":34} I want to merge and sum the values
Advertisements
How to merge two word document using java
How to merge two word document using java   how to merge two word document using java
How to merge two word document using java
How to merge two word document using java   how to merge two word document using java
ModuleNotFoundError: No module named 'two-num-sum'
ModuleNotFoundError: No module named 'two-num-sum'  Hi, My Python... 'two-num-sum' How to remove the ModuleNotFoundError: No module named 'two-num-sum' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'two-num-sum'
ModuleNotFoundError: No module named 'two-num-sum'  Hi, My Python... 'two-num-sum' How to remove the ModuleNotFoundError: No module named 'two-num-sum' error? Thanks   Hi, In your python environment
Python sum dictionary values by key
values. We are getting these two dictionaries from two different web services. We want to merge these dictionaries into one and sum the dictionary values by key...Python sum dictionary values by key  Hi, In python we can easily
Sum of two Matrix
Sum of  two Matrix       In this section, we are going to calculate the sum of two matrix... to this. In this program we are going to calculate the sum of two matrix. To make this program
write a program which asks the user for two int values and calculates their sum.
write a program which asks the user for two int values and calculates their sum.  The title says it all, an example is (1,2) > 3
Sum of two numbers using command line arguments in Java
Sum of two numbers using command line arguments in Java  Hi, How...? The calculate the sum of two numbers using command line arguments in Java... sum = a+b; Check complete example code at Add Two Numbers in Java. Thanks
sum
sum  a program to find the sum of the alternative diagit of it ex- no=123456 sum=1+3+5=9
sum
; sum=sum+s; if(i==n){ System.out.print(s...+"+"); } System.out.print("\nSum of the series : "+sum
ModuleNotFoundError: No module named 'dictionaries'
ModuleNotFoundError: No module named 'dictionaries'  Hi, My Python... 'dictionaries' How to remove the ModuleNotFoundError: No module named 'dictionaries' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'nested-dictionaries'
ModuleNotFoundError: No module named 'nested-dictionaries'  Hi, My... named 'nested-dictionaries' How to remove the ModuleNotFoundError: No module named 'nested-dictionaries' error? Thanks   Hi
split and merge
split and merge  How to merge two text files into another text file How to split a huge amount of text file into small chunks in Java
Mysql Merge
Mysql Merge       Mysql Merge Statement is used to merge the two sql statement using UNION clause. The UNION clause is used to combine the result set of any two sql queries
Mysql Merge
Mysql Merge       Mysql Merge Statement is used to merge the two sql statement using UNION clause.The UNION clause is used to combine the result set of any two sql
How do you sum a dictionary
How do you sum a dictionary  Hi, I want to sum the values of two dictionary based on keys. How do you sum a dictionary
Mysql Merge Statement
Mysql Merge Statement       Mysql Merge Statement is used to merge the two sql statement... two sql queries. Understand with Example The Tutorial covers you an example
Mysql Merge Statement
Mysql Merge Statement       Mysql Merge Statement is used to merge the two sql statement using UNION clause. The UNION clause is used to combine the result set of any two
sum in JTable
sum in JTable  how to calculate sum from JTable's one field like total
sum of the series
sum of the series  Program to sum the series 1+1/1!+1/2!+1/3!+....n
Concatenating two Strings in Java
Concatenating two Strings in Java In this section we will help you, how to concatenate two strings in java. String are sequence of character which is used... by adding  two strings. The String class include a method which concatenate
Merging Two Cells
Merging two cells       In this program we are going to merge two cells of an excel sheet through java program .You can merge any number of cells. The package we need
Sum of integers
Sum of integers  A Java program that reads an integer value from the user and displays i) the sum of all even integers between 1 and the input value, both inclusive. ii) The sum of all odd integers between 1 and the input
Merge Sort Java
. In Merge sort unsorted values are divided into two equal parts, then they are repeatedly merged till the final list is in sorted order completely. Merge Sort...Merge Sort in Java is used to sort integer values of an array. There are many
sum database column
sum database column  please i have two columns-col1 and col2, col1... 3 Soap 4 how will i query the database to sum the Items... for you. select item, sum(quqantity) from `order` group by item
JavaScript array merge
in understanding how to merge two different array into one array using Java Script. We... JavaScript array merge     ... the two array and store in a variable c Finally the document. write print
sum
sum
sum

Ads