Dictionary checking

Michael Gilfix mgilfix at eecs.tufts.edu
Mon May 27 13:20:30 EDT 2002


On Sat, May 25 @ 19:45, Bjørn Ove Grøtan wrote:
> I'm building a dictionary with values from a file.
> 
> for each key, I have an ID, Name and Alias
> for some keys I have only ID and Name, some keys with
> ID and Alias, some keys with all 3 and a few keys
> with only a ID with no Name and no Alias.
> 
> How can I check if Name and/or Alias contain anything. 
> If Name contains any string it's ok, but if not I want to
> use the string value of Alias - and if not any value (space/blank)
> of Alias I want to do some error-handling.

  It sounds like what you really want to do is have a dict within
a dict. So for example:

  dict = {
    entry = {
       'name'  : value,
       'alias' : other,
       'id'    : idno,
    },
    otherentry = {
       'name' : value,
       'id'   : idno,
    }
  }

  Now, you can check if otherentry has the record:

    if dict[otherentry].has_key ('alias'):
       # Do something
    else:
       # Do something else

> Also, I want to make some output with data combined from 2 dictionaries.
> Does anyone have any examples on how to do this?

  As others have said, dict.update () is your friend.

                   -- Mike

-- 
Michael Gilfix
mgilfix at eecs.tufts.edu

For my gpg public key:
http://www.eecs.tufts.edu/~mgilfix/contact.html





More information about the Python-list mailing list