Set of Dictionary

V

Vibha Tripathi

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
 
S

Scott David Daniels

Vibha said:
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
(e-mail address removed)
 
P

Piet van Oostrum

Vibha Tripathi said:
VT> Hi Folks,
VT> I know sets have been implemented using dictionary but
VT> I absolutely need to have a set of dictionaries...any
VT> 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)
 
R

Raymond Hettinger

[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.


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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top