How to use recursion to embed dictionaries when integrating with existing entries

I am parsing some XML data from the Open Street Map in JSON (it will later be loaded into the database). It is big, so I use iterparse. I have tags that look like this:

<tag 'k'=stringA:stringB:stringC 'v'=value>

which I want to analyze in

{stringA: {stringB: {stringC: value} } }

I did this with ugly hard code. However, I would like to create a function that uses recursion to solve the same problem if the value of the attribute "k" contains an arbitrary number ":".

I created this

def nestify(l):
    """Takes a list l and returns nested dictionaries where each element from
    the list is both a key to the inner dictionary and a value to the outer, 
    except for the last element of the list, one which is only a value.
    For best understanding, feed w = [ 'a', 'b', 'c', 'd', 'e', 'f', 'v'] to the
    function and look at the output."""
    n = len(l)
    if n==2:
        key = l[0]
        value = l[1]
    else:
        key = l[0]
        value = nestify(l[1:])

    return {key:value}                   

which is working. (You must first make the keys and value in the list.) Also, that is not the case, because he always makes a new dictionary. I need him to respect the previous data and integrate it. For example, if my parser is found

<tag 'k'=a:b:c 'v'=1 />

and then in the same element

<tag 'k'=a:d:e 'v'=2 />

{a: {'b': {'c' : 1}, 'd' : {'e' : 2}}}

{a: {'b' : {'c' : 1}}}

{a: {'d' : {'e' : 2}}}

:

def smart_nestify(l, record):
n = len(l)
if n==2:
    key = l[0]
    value = l[1]
else:
    key = l[0]
    value = smart_nestify(l[1:], key)
if key not in record:
    return {key:value}                   
else:
    record[key] = value
    return record

. ? ?

+4
2

" " nestify, l record:

def smarter_nestify(l, record):
    if len(l) == 2:
        return {l[0]: l[1]}
    key = l.pop(0)
    record[key] = smarter_nestify(l, record.get(key, {}))
    return record

l.pop(0) record.get(key, {}).

, :

l = ['a', 'b', 'c', 1]
record = smarter_nestify(l, {})

l = ['a', 'd', 'e', 2]
record = smarter_nestify(l, record)
print record
0

, . .

g_values = dict()
def make_nested(l):
    """
    l is the array like you have for nestify
    e.g., ['a',  'b', 'c', 1 ] or ['a', 'd', 'e', 2]
    """
    global g_values
    value = l[-1] ; l = l[:-1]
    last_dict = None
    for x in l[:-1]:
        mp_temp = last_dict if last_dict is not None or g_values
        last_dict = mp_temp.get(x)
        if last_dict is None:
            mp_temp[x] = dict()
            last_dict = mp_temp[x]
    last_dict[l[-1]] = value

, - :

def make_nested(values, l):
    if len(l == 2):
        values[l[0]] = l[1]
        return
    mp = values.get(l[0])
    if mp is None:
        values[l[0]] = dict()
        make_nested(values[l[0]], l[1:])
    else:
        make_nested(mp, l[1:])       
-1

All Articles