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));
|
Create dataframe from nested dictionary keeping nested dictionary as column
Tag : python , By : Chris Hanley
Date : March 29 2020, 07:55 AM
this will help That's not the greatest input data, but if you're confident that the structure of the dictionary won't change, you could try something like the below: DataFrame([
[x[0] for x in nested_dict.keys()],
[x[1] for x in nested_dict.keys()],
[x for x in nested_dict.values()],
]).T
0 1 2
0 timestamp4 324 {326: 3}
1 timestamp3 323 {325: 2}
2 timestamp 321 {321: 0}
3 timestamp2 322 {323: 1}
|
How can I create a flat dictionary from a nested dictionary whose keys are a subset of a reference dictionary?
Date : March 29 2020, 07:55 AM
around this issue Using a generator, this is fairly straight forward: Code: def make_flat_tuples(data, ref):
for k, v in data.items():
if isinstance(v, dict):
for x in make_flat_tuples(v, ref[k]):
yield x
else:
yield ref[k], v
flat = dict(make_flat_tuples(nested, reference))
from collections import defaultdict
reference = defaultdict(dict)
reference['agent'] = defaultdict(dict)
reference['agent']['address'] = 'agentaddress'
reference['agent']['zone']['id'] = 'agentzoneid'
reference['eventid'] = 'eventid'
reference['file']['hash'] = 'filehash'
reference['file']['name'] = 'filename'
nested = defaultdict(dict)
nested['agent']['address'] = '172.16.16.16'
nested['eventid'] = '1234566778'
nested['file']['name'] = 'reallybadfile.exe'
print(dict(make_flat_tuples(nested, reference)))
{
'agentaddress': '172.16.16.16',
'eventid': '1234566778',
'filename': 'reallybadfile.exe'
}
|
update nested dictionary values from another nested dictionary based on mapping already provided in the dictionary
Date : March 29 2020, 07:55 AM
this one helps. In your function find_input_value you are not propagating the output through the recursions properly. Recursive functions need to have consistent return values. You have 2 returns that send a result and 1 that sends a None. Here is a version that works for me, even though I suspect it can be simplified. def find_input_value(k,input_json):
if k in input_json:
val = input_json[k]
for v in input_json.values():
if 'val' in locals(): #check if val has been defined
if val is not None: #needs to be defined and not None
break # kill the loop cause found it
if isinstance(v, dict):
val = find_input_value(k,v)
return val if 'val' in locals() else None # if input_json is not dict return None
|
question regarding a nested dictionary is there a way to merge a nested dictionary in one dictionary
Date : March 29 2020, 07:55 AM
With these it helps I'm following a python course on runestone and i'm stuck with the following question: , This should do it: pokemon_total = {}
for player, dictionary in pokemon_go_data.items():
for pokemon, candy_count in dictionary.items():
if pokemon in pokemon_total.keys():
pokemon_total[pokemon] += candy_count
else:
pokemon_total[pokemon] = candy_count
most_common_pokemon = max(pokemon_total, key=pokemon_total.get)
print(most_common_pokemon)
|