Faqts : Computers : Programming : Languages : Python : Snippets : Dictionaries

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

12 of 27 people (44%) answered Yes
Recently 2 of 10 people (20%) answered Yes

Entry

Extracting tuple values from a dictionary

Sep 15th, 2009 02:25
jick heir, Nathan Wallace, unknown unknown, Hans Nowak, Snippet 71, Magnus L. Hetland


"""
Packages: basic_datatypes.dictionaries
"""
"""
> For the specific example I gave, formatting works fine, but what about
> other situations, such as extracting a tuple of values from a hash: 	a,
> b, c = value{key1,key2,key3}
It seems you may want to define a dictionary of your own, here...
"""
from types import TupleType
from UserDict import UserDict
class TupleDict(UserDict):
    def __init__(self,data):
        UserDict.__init__(self)
        self.data = data
    def __getitem__(self,key):
        if type(key) is TupleType:
            result = []
            for k in key:
                result.append(self[k])
            return tuple(result)
        return self.data[key]
"""
That should work the way you want.
"""
http://sundhedernaeringguide.blogspot.com/
http://zdravevyzivypruvodce.blogspot.com/
http://guidasalutenutrizione.blogspot.com/
http://gesundheiternahrungpflege.blogspot.com/
http://soinssantenutrition.blogspot.com/
http://zdravljeprehranavodic.blogspot.com/
http://healthnutritionhoito.blogspot.com/
http://hlhnutgr.blogspot.com/
http://gezondheidvoedinggids.blogspot.com/
http://taplalkozas-egeszsegugyikezikonyv.blogspot.com/
http://hlthnutbg.blogspot.com/