dict's as dict's key.

Discussion in 'Python' started by Albert van der Horst, Jan 13, 2010.

  1. I have a type of objects that have complicated enough properties
    to warrant a special class for its type.
    The class has built in dictionary for all the properties.

    Something along the line of
    a = ctype({"poker":True})
    b = ctype({"footbal":True, "gender":"m"})
    c = ctype({"chess":True, "residence":"Amsterdam"})
    I can count each type, again using a dictionary:
    db = {}
    db[a]=171
    db=208

    But now I am at a loss as how to look up a ctype z in this db
    dictionary efficiently, because all those objects are different
    from z.

    Is there a way to turn those ctype things into a hashable type?
    (I would then convert z in the same way.)
    Once a ctype is invented it never changes.
    The only data pertinent to a ctype is its property dictionary.

    (I encountered this before. A dictionary is a natural for a
    boardgame position, i.e. chess. Now we want to look up chess
    positions.)

    Groetjes Albert

    --
    --
    Albert van der Horst, UTRECHT,THE NETHERLANDS
    Economic growth -- being exponential -- ultimately falters.
    albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst
    Albert van der Horst, Jan 13, 2010
    #1
    1. Advertising

  2. Albert van der Horst <4all.nl> writes:

    > I have a type of objects that have complicated enough properties
    > to warrant a special class for its type.
    > The class has built in dictionary for all the properties.
    >
    > Something along the line of
    > a = ctype({"poker":True})
    > b = ctype({"footbal":True, "gender":"m"})
    > c = ctype({"chess":True, "residence":"Amsterdam"})
    > I can count each type, again using a dictionary:
    > db = {}
    > db[a]=171
    > db=208
    >
    > But now I am at a loss as how to look up a ctype z in this db
    > dictionary efficiently, because all those objects are different
    > from z.
    >
    > Is there a way to turn those ctype things into a hashable type?
    > (I would then convert z in the same way.)
    > Once a ctype is invented it never changes.
    > The only data pertinent to a ctype is its property dictionary.
    >


    Something like this will work (untested):

    class ctype(object):
    def __init__(self, propdict):
    self.propdict = propdict
    self._hash = hash(frozenset(propdict.items()))
    def __hash__(self):
    return self._hash
    def __eq__(self, other):
    return isinstance(other, ctype) and self.propdict == other.propdict

    Note: you should capitalize your class names if you want to comply with
    PEP 8.

    HTH

    --
    Arnaud
    Arnaud Delobelle, Jan 13, 2010
    #2
    1. Advertising

  3. Albert van der Horst

    Peter Otten Guest

    Albert van der Horst wrote:

    > I have a type of objects that have complicated enough properties
    > to warrant a special class for its type.
    > The class has built in dictionary for all the properties.
    >
    > Something along the line of
    > a = ctype({"poker":True})
    > b = ctype({"footbal":True, "gender":"m"})
    > c = ctype({"chess":True, "residence":"Amsterdam"})
    > I can count each type, again using a dictionary:
    > db = {}
    > db[a]=171
    > db=208
    >
    > But now I am at a loss as how to look up a ctype z in this db
    > dictionary efficiently, because all those objects are different
    > from z.
    >
    > Is there a way to turn those ctype things into a hashable type?
    > (I would then convert z in the same way.)
    > Once a ctype is invented it never changes.
    > The only data pertinent to a ctype is its property dictionary.


    >>> class CType:

    .... def __init__(self, data):
    .... self.data = data
    .... self._hash = hash(tuple(sorted(data.iteritems())))
    .... def __hash__(self):
    .... return self._hash
    .... def __eq__(self, other):
    .... return self._hash == other._hash and self.data == other.data
    ....
    >>> a = CType({"poker":True})
    >>> b = CType({"footbal":True, "gender":"m"})
    >>> c = CType({"chess":True, "residence":"Amsterdam"})
    >>> db = {}
    >>> db[a]=171
    >>> db=208
    >>> db[CType(dict(poker=True))]

    171
    >>> CType(dict(poker=False)) in db

    False

    Be very careful not to change the dictionary in the data attribute.
    Otherwise you'll break your application.

    Peter
    Peter Otten, Jan 13, 2010
    #3
  4. Albert van der Horst

    Lie Ryan Guest

    On 01/14/10 05:33, Albert van der Horst wrote:
    > (I encountered this before. A dictionary is a natural for a
    > boardgame position, i.e. chess. Now we want to look up chess
    > positions.)


    or use collections.namedtuple
    Lie Ryan, Jan 16, 2010
    #4
  5. Albert van der Horst

    Ethan Furman Guest

    Lie Ryan wrote:
    > On 01/14/10 05:33, Albert van der Horst wrote:
    >
    >>(I encountered this before. A dictionary is a natural for a
    >>boardgame position, i.e. chess. Now we want to look up chess
    >>positions.)

    >
    >
    > or use collections.namedtuple


    Which is great until you want to make a move. ;)

    ~Ethan~
    Ethan Furman, Jan 17, 2010
    #5
  6. Albert van der Horst

    Lie Ryan Guest

    On 01/17/10 12:29, Ethan Furman wrote:
    > Lie Ryan wrote:
    >> On 01/14/10 05:33, Albert van der Horst wrote:
    >>
    >>> (I encountered this before. A dictionary is a natural for a
    >>> boardgame position, i.e. chess. Now we want to look up chess
    >>> positions.)

    >>
    >>
    >> or use collections.namedtuple

    >
    > Which is great until you want to make a move. ;)


    No problem, just create a new namedtuple.

    The OP wanted fast attribute lookup and AFAICT doesn't mention anything
    about fast construction.
    Lie Ryan, Jan 17, 2010
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Skip Montanaro
    Replies:
    0
    Views:
    412
    Skip Montanaro
    Aug 15, 2003
  2. Christoph Groth

    dict: retrieve the original key by key

    Christoph Groth, May 15, 2011, in forum: Python
    Replies:
    0
    Views:
    195
    Christoph Groth
    May 15, 2011
  3. Christoph Groth

    Re: dict: retrieve the original key by key

    Christoph Groth, May 15, 2011, in forum: Python
    Replies:
    5
    Views:
    291
    Ian Kelly
    May 16, 2011
  4. M P
    Replies:
    1
    Views:
    465
  5. Victor Hooi
    Replies:
    1
    Views:
    108
    Victor Hooi
    Oct 29, 2013
Loading...

Share This Page