calculate the total value dict

Yanwar Sky

I have a problem, how to calculate the total dict of the same keys ? I have a dict:

{'learning': {'DOC1': 0.14054651081081646,
              'DOC2': 0,
              'DOC3': 0.4684883693693881},
 'life':     {'DOC1': 0.14054651081081646, 
              'DOC2': 0.20078072972973776, 
              'DOC3': 0}
}

and I hope the results as:

{'learning life': {
        'DOC1': DOC1 in learning + DOC1 in life,
        'DOC2': DOC2 in learning + DOC2 in life,
        'DOC3': DOC3 in learning + DOC3 in life,}}

Thank you very much

Malik Brahimi

You can use a dictionary comprehension to add all the numbers nested in a dictionary d just like so:

totals = {k: sum(v.get(k, 0) for v in d.values()) for k in d.values()[0]} # dict of totals

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

automatically calculate total value using jQuery

From Dev

Calculate total value in table row, JavaScript jQuery

From Dev

Calculate total using hidden field value instead of checkbox value

From Dev

Using $sum to calculate the total value in a object, mongoose/mongodb

From Dev

Calculate total time for which the value was 1 for a specified time period

From Dev

Pivot columns with aggregated function and calculate total of aggregated value in last column

From Dev

Calculate the max total price of rods, cut with unique length value

From Dev

How to calculate % of total in MySQL

From Dev

Calculate sub total emberjs

From Dev

mssql calculate total by hour

From Dev

Calculate total price with 'WITH RECURSIVE'

From Dev

Calculate proper total in query?

From Dev

EmberJs Calculate the total amount

From Dev

Calculate sub total emberjs

From Dev

How to calculate % of total in MySQL

From Dev

Calculate and divide by total with AWK

From Dev

Calculate total price with 'WITH RECURSIVE'

From Dev

Calculate total on checkbox checked

From Dev

Calculate running total in JTable

From Dev

Calculate total hours in Rails

From Dev

Calculate total time in oracle

From Dev

Calculate the total of prices in angularjs

From Dev

Javascript: how to calculate the total

From Dev

Calculate classes' percentage with dict?

From Dev

Dict KeyError for value in the dict

From Dev

When i click button ? Inserted value in edittext should be total and Calculate to display in another edittext with the help SELECT QUERY?

From Dev

count the total time of values in textbox. i want to calculate the time of the value

From Dev

Calculate Grand Total from sub-total

From Dev

Calculate running total per month

Related Related

HotTag

Archive