[Vibha]
> I know sets have been implemented using dictionary but
> I absolutely need to have a set of dictionaries...any
> ideas how to do that?

Yes.  Create a dictionary subclass that is hashable.  Be sure not to
mutate it after using it in a set.



>>> class FrozenDict(dict):
    def __hash__(self):
        try:
            return self._hash
        except AttributeError:
            self._hash = hash(tuple(sorted(self.iteritems())))
            return self._hash


>>> d1 = FrozenDict(a=1, b=2, c=3)
>>> d2 = FrozenDict(d=4, e=5, f=6)
>>> d3 = FrozenDict(b=2, c=3, a=1)
>>> s = set([d1, d2, d3])
>>> s
set([{'e': 5, 'd': 4, 'f': 6}, {'a': 1, 'c': 3, 'b': 2}])
>>> d2 in s
True


Raymond Hettinger

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to