440,035 Members | 1,984 Online
Need help? Post your question and get tips & solutions from a community of 440,035 IT Pros & Developers. It's quick & easy.

# Set of Dictionary

 P: n/a Hi Folks, I know sets have been implemented using dictionary but I absolutely need to have a set of dictionaries...any ideas how to do that? Peace. Vibha "Things are only impossible until they are not." __________________________________ Discover Yahoo! Find restaurants, movies, travel and more fun for the weekend. Check it out! http://discover.yahoo.com/weekend.html Jul 19 '05 #1
4 Replies

 P: n/a Vibha Tripathi wrote: Hi Folks, I know sets have been implemented using dictionary but I absolutely need to have a set of dictionaries...any ideas how to do that? Peace. Vibha "Things are only impossible until they are not." __________________________________ Discover Yahoo! Find restaurants, movies, travel and more fun for the weekend. Check it out! http://discover.yahoo.com/weekend.html This is too bad. There is a problem even defining what you want. Assume we have a DictSet type: After: a = dict(a=1, b=2) b = dict(a=2, b=1) c = dict(b=2, a=1) ds = Dictset([a, b, c]) What is len(ds)? After those lines and: c['a'] = 2 What is len(ds)? After all the previous lines and: a['a'] = 2 b['b'] = 2 What is len(ds)? Making sets of mutable things is pretty useless. You could make sets of tuple(sorted(adict.items())) if the "adict"s don't have mutable values. --Scott David Daniels Sc***********@Acm.Org Jul 19 '05 #2

 P: n/a >>>>> Vibha Tripathi (VT) wrote: VT> Hi Folks,VT> I know sets have been implemented using dictionary butVT> I absolutely need to have a set of dictionaries...anyVT> ideas how to do that? A set cannot contain duplicates. What happens when one of the dictionaries in the set is modified in such a way that it becomes equal to another element? Maybe you just need a list of dictionaries? Or maybe you want immutable dictionaries to put in the set. In that case you can define your own immutable dictionary class that defines a hash function. Ideally you would redefine the dictionary modification methods to make them generate error messages. For safety in the following code a copy is made of the dict. The values in the dict must be hashable, which usually means immutable. class immdict(dict): def __init__(self, somedict): self.data = somedict.copy() def __hash__(self): result = 0 for elt in self.data: result ^= hash(elt) ^ hash(self.data[elt]) return result def __repr__(self): return repr(self.data) __str__ = __repr__ d = immdict({1:0, 2:3}) s = Set() s.add(d) -- Piet van Oostrum URL: http://www.cs.uu.nl/~piet [PGP] Private email: pi**@vanoostrum.org Jul 19 '05 #3

 P: n/a See the frozendict recipe: http://aspn.activestate.com/ASPN/Coo.../Recipe/414283 It was written exactly for this purpose: a dictionary that can be a member in a set. Oren Jul 19 '05 #4

 P: n/a [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 Jul 19 '05 #5

### This discussion thread is closed

Replies have been disabled for this discussion.