logo
down
shadow

Sum up the second value from one dictionary with all values from another dictionary


Sum up the second value from one dictionary with all values from another dictionary

Content Index :

Sum up the second value from one dictionary with all values from another dictionary
Tag : python , By : Brandon
Date : November 25 2020, 03:01 PM

seems to work fine I have two dictionaries, with the same keys, but different values. I want to make a new dictionary with all values from dictionary1 to be summed up with the second value of dictionary2. , I solved it. For future if anyone needs the answer, this did the job:
for key in dic1:
    for i in range(0,len(dic1[key])):
        if len(dic1[key]) > 0:
            dic1[key][i] = dic1[key][i] + dic2[key][1]
dic3[key][i] = dic1[key][i] + dic2[key][1]

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

C# Sum of Values in a nested dictionary where value is a class Dictionary<string, Dictionary<Int16, CommonData>


Tag : chash , By : demize95
Date : March 29 2020, 07:55 AM
I hope this helps . You didn't give some example input/output but I think your looking for something like
var result = yourMainDict.OrderBy(x => x.Value.Sum(v => v.Value.Delivered + v.Value.Submitted + v.Value.Failed));

How to replace dictionary values which is dictionary with tuple for each dictionary in list


Tag : python , By : Sascha Brossmann
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , , This isn't working because when you do:
D = [dictionary for i in range(10)]
i['c'] = tuple(i['c'].items())
In [10]: dictionary = { "a" : "100", "b" : "200", "c" : {"a":"f","b":"4"} }
    ...: D = [dictionary for i in range(10)]
    ...: print([hex(id(x)) for x in D])
    ...:
['0x105c59088', '0x105c59088', '0x105c59088', '0x105c59088', '0x105c59088', '0x105c59088', '0x105c59088', '0x105c59088', '0x105c59088', '0x105c59088']
In [11]: dictionary = { "a" : "100", "b" : "200", "c" : {"a":"f","b":"4"} }
    ...: D = [dictionary.copy() for i in range(10)]
    ...: print([hex(id(x)) for x in D])
    ...:
['0x105c592c8', '0x105c59e48', '0x105cfd848', '0x105c9af48', '0x105d06c48', '0x105c59708', '0x105d06cc8', '0x105c59488', '0x105c59e08', '0x105c593c8']
In [12]: for i in D:
    ...:     i['c'] = tuple(i['c'].items())
    ...:

In [13]: D
Out[13]:
[{'a': '100', 'b': '200', 'c': (('b', '4'), ('a', 'f'))},
 {'a': '100', 'b': '200', 'c': (('b', '4'), ('a', 'f'))},
 {'a': '100', 'b': '200', 'c': (('b', '4'), ('a', 'f'))},
 {'a': '100', 'b': '200', 'c': (('b', '4'), ('a', 'f'))},
 {'a': '100', 'b': '200', 'c': (('b', '4'), ('a', 'f'))},
 {'a': '100', 'b': '200', 'c': (('b', '4'), ('a', 'f'))},
 {'a': '100', 'b': '200', 'c': (('b', '4'), ('a', 'f'))},
 {'a': '100', 'b': '200', 'c': (('b', '4'), ('a', 'f'))},
 {'a': '100', 'b': '200', 'c': (('b', '4'), ('a', 'f'))},
 {'a': '100', 'b': '200', 'c': (('b', '4'), ('a', 'f'))}]

In python, I want to sort a dictionary based on the dictionary of dictionary values


Tag : python , By : Chris Lomax
Date : March 29 2020, 07:55 AM
help you fix your problem In python I have a dictionary of dictionary such as , You can use a custom key for sorting:
print(dict(sorted(my_dict.items(), key=lambda x: x[1]['mark3'], reverse=True)))
{'C': {'mark1': '11', 'mark2': '19', 'mark3': '25'}, 'A': {'mark1': '15', 'mark2': '12', 'mark3': '20'}, 'B': {'mark1': '10', 'mark2': '90', 'mark3': '14'}}
from collections import OrderedDict
my_dict = OrderedDict([('A', {'mark1': '15', 'mark2': '12', 'mark3': '20'}), ('B', {'mark1': '10', 'mark2': '90', 'mark3': '14'}), ('C', {'mark1': '11', 'mark2': '19', 'mark3': '25'})])
print(OrderedDict(sorted(my_dict.items(), key=lambda x: x[1]['mark3'], reverse=True)))
print(dict(sorted(my_dict.items(), key=lambda x: (x[1]['mark3'], x[1]['mark2']), reverse=True)))

Replacing the keys of a dictionary with values of another dictionary to create a new dictionary


Tag : python , By : Cenneca
Date : March 29 2020, 07:55 AM
hope this fix your issue Your attempts rewrite the data at each iteration. Plus they lose the relation between the name and the index by only iterating on the values. Time to rethink the approach from scratch.
I would combine both dict data and build a new dictionary using dictionary comprehension:
result = {name:ages[index] for index,name in d1.items()}

>>> result
{'Bill': 20, 'Bob': 40, 'Callum': 20, 'Herrera': 21, 'Liz': 19}
result = {name:ages.get(index,"unknown") for index,name in d1.items()}

How to modify or create a dictionary based on one dictionary querying the values of another dictionary in Python


Tag : python , By : Martin
Date : March 29 2020, 07:55 AM
I wish this helpful for you I have two dictionaries: Dictionary A has an increasing and unique Item ID as the key and it is associated to an FID value. , try this code:
dict_a = dict({11111: "FID01", 22222: "FID01", 33333:"FID03", 44444: "FID01"} )
dict_b = dict({"FID01": 25, "FID02": 50, "FID03": 75, "FID04": 90})

dict_b_keys = dict_b.keys()

{key: dict_b[value] for (key, value) in dict_a.items() if value in dict_b_keys}
Related Posts Related QUESTIONS :
  • How to Pack with PyQt - how to make QFrame/Layout adapt to content
  • How do I get certain Time Range in Python
  • python doubly linked list - insertAfter node
  • Open .h5 file in Python
  • Joining a directory name with a binary file name
  • python, sort list with two arguments in compare function
  • Is it possible to print from Python using non-ANSI colors?
  • Pandas concat historical data using date minus some number of days
  • CV2: Import Error in Python OpenCV
  • Is it possible to do this loop in a one-liner?
  • invalid literal for int() with base 10: - django
  • Why does my code print a value that I have not assigned as yet?
  • the collatz func in automate boring stuff with python
  • How to find all possible combinations of parameters and funtions
  • about backpropagation deep neural network in tensorflow
  • Sort strings in pandas
  • How do access my flask app hosted in docker?
  • Replace the sentence include some text with Python regex
  • Counting the most common element in a 2D List in Python
  • logout a user from the system using a function in python
  • mp4 metadata not found but exists
  • Django: QuerySet with ExpressionWrapper
  • Pandas string search in list of dicts
  • Decryption from RSA encrypted string from sqlite is not the same
  • need of maximum value in int
  • a list of several tuples, how to extract the same of the first two elements in the small tuple in the large tuple
  • Display image of 2D Sinewaves in 3D
  • how to prevent a for loop from overwriting a dictionary?
  • How To Fix: RuntimeError: size mismatch in pyTorch
  • Concatenating two Pandas DataFrames while maintaining index order
  • Why does this not run into an infinite loop?
  • Python Multithreading no current event loop
  • Element Tree - Seaching for specific element value without looping
  • Ignore Nulls in pandas map dictionary
  • How do I get scrap data from web pages using beautifulsoup in python
  • Variable used, golobal or local?
  • I have a regex statement to pull all numbers out of a text file, but it only finds 77 out of the 81 numbers in the file
  • How do I create a dataframe of jobs and companies that includes hyperlinks?
  • Detect if user has clicked the 'maximized' button
  • Does flask_login automatically set the "next" argument?
  • Indents in python 3
  • How to create a pool of threads
  • Pandas giving IndexError on one dataframe but not on another similar dataframe
  • Django Rest Framework - Testing client.login doesn't login user, ret anonymous user
  • Running dag without dag file in airflow
  • Filling across a specified dimension of a numpy array
  • Python populating dataframe in pandas from text files
  • How to interpolate a single ("non-piecewise") cubic spline from a set of data points?
  • Divide 2 integers (leetcode 29) - recursion issue
  • Can someone explain why do I get this output in Python?
  • How do I scrape pdf and html from search results without obvious url
  • Is there a way to automatically make a "collage" of plots with matplotlib?
  • How to combine multiple rows in pandas with shared column values
  • How do I get LOAD_CLASSDEREF instruction after dis.dis?
  • Django - How to add items to Bootstrap dropdown?
  • Linear Regression - Does the below implementation of ridge regression finding coefficient term using gradient method is
  • How to drop all rows in pandas dataframe with negative values?
  • Most Efficient Way to Find Closest Date Between 2 Dataframes
  • Execution error when Passing arguments to a python script using os.system. The script takes sys.argv arguments
  • Looping through a function
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com