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
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
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}
|