Is there any elegant way to build a multi-level dictionary in python?
Date : March 29 2020, 07:55 AM
I hope this helps . This uses the copy function to allow you specify a different leaf node. Otherwise all the leaves will point to the same dictionary. from copy import copy
def multidict(*args):
if len(args) == 1:
return copy(args[0])
out = {}
for x in args[0]:
out[x] = multidict(*args[1:])
return out
print multidict(['a', 'b'], ['A', 'B'], ['1', '2'], {})
|
Android studio top level build.gradle file - how to set property of lower level build file
Date : March 29 2020, 07:55 AM
Any of those help It is not the same. As I know you can't define the gradle file with a setProperty method. You can define a custom name in the settings.gradle file. Something like: include ':app'
project(':app').buildFileName = 'mybuild.gradle'
|
Build a dictionary in the form of a multi-level tree
Tag : python , By : Simon Capewell
Date : March 29 2020, 07:55 AM
wish of those help you can keep track of a mapping of ID to where it is in the tree and another for the actual tree. db_tree = [
{"id":2, "parent_id":1, "level":1, "name":"parent 1"},
{"id":5, "parent_id":2, "level":2, "name":"child 1 - 1"},
{"id":6, "parent_id":2, "level":2, "name":"child 1 - 2"},
{"id":9, "parent_id":2, "level":2, "name":"child 1- 3"},
{"id":7, "parent_id":5, "level":3, "name":"child 1 - 1 - 1"},
{"id":11, "parent_id":6, "level":3, "name":"children 2- 1"},
{"id":10, "parent_id":7, "level":4, "name":"child 4 levl parent 1"},
{"id":3, "parent_id":1, "level":1, "name":"parent 2"},
{"id":13, "parent_id":3, "level":2, "name":"parent 2- 1 - chil"},
{"id":4, "parent_id":1, "level":1, "name":"parent 3"},
{"id":8, "parent_id":1, "level":1, "name":"parent 4"}
]
tree = {}
id_to_children = {1: tree}
for entry in db_tree:
id = entry["id"]
name = entry["name"]
parent_id = entry["parent_id"]
# assume all elements are new, could check if it already exists
my_children = {}
id_to_children[id] = my_children
#this is where we should be added in the tree.
parent_children = id_to_children[parent_id]
# add our node as a new child of our parent.
parent_children[name] = my_children
import pprint
pprint.pprint(tree)
|
Flattening a multi-level dictionary into single level dictionary
Date : March 29 2020, 07:55 AM
Hope this helps I want to flatten a multi-level dictionary into a single level dictionary. The new single levels keys should be concatenated with a separator "_". It's not a duplicate of the other flatten dict question since this has arrays that are concatenated into a string properly separated. , The following should work: Code: d_new = dict()
for k, v in d.items():
for k2 in set(k for d in v for k in d.keys()):
d_new[f"{k}_{k2}"] = '; '.join(d2.get(k2, '') for d2 in v)
>>> d_new
{'dates_unparsed_date': '; 1987; ',
'dates_date': '1970-08-01; ; 2000-00-00',
'dates_type': 'Date of birth; Year of birth; Date of birth2'}
>>> {f"{k}_{k2}" : '; '.join(d2.get(k2, '') for d2 in v) for k, v in d.items() for k2 in set(k for d in v for k in d.keys())}
{'dates_unparsed_date': '; 1987; ',
'dates_date': '1970-08-01; ; 2000-00-00',
'dates_type': 'Date of birth; Year of birth; Date of birth2'}
|
Jenkins Build Error: Build step 'Invoke top-level Maven targets' marked build as failure
Tag : jenkins , By : George Handlin
Date : March 29 2020, 07:55 AM
|