Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Pythong Dictionaries

Name: Anonymous 2011-03-16 18:21

I can't figure out how to do this, I tried recursion but I'm horrible at it. I wouldn't mind trying this out iteratively either, I'm not looking for what's best, I'm just trying to get the job done.

Say I have a dictionary with dictionaries in them, dicts = {'a':{'b':{'c':None}, how would I add a dictionary to it assuming I didn't know how many dictionaries in it was?

Name: Anonymous 2011-03-16 19:52

>>7 OK, now I see what you want.

def add(d, s):
    if not len(s): return # we are done with it.
    c, tail = s[0], s[1:]
    if c in d:
        v = d[c]
        if isinstance(v, basestring): # you're going to replace it with your is_leaf trie method
            d[c] = make_trie_from_two_strings(v, tail) # implement yourself
        else:
            add(v, tail)
    else:
        d[c] = tail

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List