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

Pages: 1-

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 18:31

Are you assuming that each dictionary only has one item? Seems a bit stupid to me.
def insert_into_dictionary_chain(dictchain,item):
    # untested
    only_item = dictchain.keys()[0]
    if dictchain[only_item]:
        insert_into_dictionary_chain(dictchain[only_item],item)
    else:
       dictchain[only_item] = item

Name: Anonymous 2011-03-16 18:40

The dictionary can have multiple item, it's built from one character from a word. So if O was to build one for the words bar and ban it would look like {'b':{'a':{'r':None}, {'n':None}}}

Name: Anonymous 2011-03-16 18:41

I mean if I was to build one, not O.

Name: Anonymous 2011-03-16 18:55

what are you actually trying to do? have you ever taken a class in data structures? is the dictionary structure you're trying to create going to both an efficient and easy-to-implement way to achieve your goal?

if so, i recommend writing classes that extend a dictionary, and then functions like add() to work with that dict-of-dicts.

in fact, i have a handful of scripts that scrape web data and store their information just like that.

Name: Anonymous 2011-03-16 19:10

OP, you just haven't set yourself the right questions. I'm assuming you want to insert a string :
How do I insert the empty string?
How do I insert the string of length one?
Assuming I can insert a string of length n, how can I insert a string of length n+1?

Name: Anonymous 2011-03-16 19:14

We've gone over data structures a bit in class, I don't have the best understanding of it because the prof isn't that good.

The actual function I'm trying to program is to add nodes to a tree (http://pastebin.com/MK6Bxvmt), but I'm not allowed to actually touch that file,it's in another file that uses it. The program will create trees based on whatever words you use and then store the word in the final node.

So if I had the words good and god the final product would look something like this:
{'g':{'o':{'d':None},{'o':{'d':None}}}})

The problem is that I don't know how to insert it this way, I'm not sure how to add key inside of x number of nested dictionaries.

Name: Anonymous 2011-03-16 19:18

Yo dawg, so we herd u liek dictionaries...

Name: Anonymous 2011-03-16 19:21

I don't have the best understanding of it because the prof isn't that good.
Translation: I'm too much of a chickenshit to ask when I don't understand something and went home clueless.

Name: Anonymous 2011-03-16 19:31

>>9
He doesn't get anything right, he's constantly corrected by other students.

Name: Anonymous 2011-03-16 19:32

oh so this is a homework thread
let me make this very clear
COCKS

Name: Anonymous 2011-03-16 19:44

>>11
I don't want the function to be written for me, I just want to know how to get to a certain dictionary in dictionary, but without knowing how many dictionaries there are.

Name: Anonymous 2011-03-16 19:52

dict['g']
dict['g']['o']
dict['g']['o']['o']

Oh if only there were some sort of pattern to this madness

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

Name: Anonymous 2011-03-16 19:53

>>13
But how would you do that without knowing how many dictionaries deep it could be? That's on the assumption you already knew.

Name: Anonymous 2011-03-16 19:59

>>14
Okay, I see what you're doing here, I'll try implementing it now. Thank you for the help.

Name: >>6 2011-03-16 20:16

>>14
If he had answered my questions he would have came to that by himself or perhaps I'm deluding myself?

Name: Anonymous 2011-03-16 20:24

>>16
You are welcome, I hope you will not drop out despite not understanding this stuff, thus decreasing the average ability of programmers and making my future salary a bit higher ^^

Also, there's an important nuance there, regarding that first fast bail out, it might have to be amended, because a trie is different from just nested dictionaries: I don't see how you can express trie('x', '') as such. That is, each node in trie should have an additional value showing that there's a string ending at that node.

Though your professor's code doesn't have anything like that, so it looks like a blind teaching the blinds, and I'm, again, happy to ensure my job security by participating!

Name: Anonymous 2011-03-16 23:14

>>18
You are welcome, I hope you will not drop out despite not understanding this stuff, thus decreasing the average ability of programmers and making my future salary a bit higher ^^
(-︢  ̱-︡   )

Name: Anonymous 2011-03-17 0:02

(cdr (cdr (cdr value)))

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