[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Can't get items out of a set?

AdamB

3/7/2008 7:13:00 PM

Hello, all.

Is it possible to get an object out of a set() given another object
that has the same hash code and equality (__hash__() and __eq__()
return the same)?

You can't do this with Java Sets either and I've needed it on multiple
occasions. Doesn't it seem like it would be useful? Consider:

class Person:
def __init__(self, id, name):
self.id = id
self.name = name

def __hash__(self):
return self.id

def __eq__(self, other):
return self.id == other.id


people = set( [Person(1, 'Joe'), Person(2, 'Sue')] )
....
p = people.get_equivalent(2) #method doesn't exist as far as I know
print p.name #prints Sue


I'm not sure if the above code compiles but I hope you get the idea.
Is it possible?
Much Thanks.
- Cruxic
7 Answers

Raymond Hettinger

3/7/2008 7:21:00 PM

0

[Cruxic]
> Is it possible to get an object out of a set() given another object
> that has the same hash code and equality (__hash__() and __eq__()
> return the same)?

Yes, but it requires an indirect approach.
http://aspn.activestate.com/ASPN/Cookbook/Python/Rec...


Raymond

Alan G Isaac

3/8/2008 3:32:00 PM

0

Cruxic wrote:

> people = set( [Person(1, 'Joe'), Person(2, 'Sue')] )

> ...

> p = people.get_equivalent(2) #method doesn't exist as far as I know

> print p.name #prints Sue



def get_equivalent(test, container):

for p in container:

if p == test:

return p



hth,

Alan Isaac





#example (note change in __eq__ to match your case; fix if nec)



class Person:

def __init__(self, id, name):

self.id = id

self.name = name

def __hash__(self):

return self.id

def __eq__(self, other):

return self.id == other



people = set( [Person(1, 'Joe'), Person(2, 'Sue')] )



get_equivalent(2,people)

AdamB

3/8/2008 4:25:00 PM

0

On Mar 7, 11:20 am, Raymond Hettinger <pyt...@rcn.com> wrote:
> [Cruxic]
>
> > Is it possible to get an object out of a set() given another object
> > that has the same hash code and equality (__hash__() and __eq__()
> > return the same)?
>
> Yes, but it requires an indirect approach.http://aspn.activestate.com/ASPN/Cookbook/Python/Rec...
>
> Raymond

That's a clever work around. Thanks, Raymond. Clearly you had a need
for this. Do you feel it's a common need that should be submitted as
a Python feature request? To me it seems like such a simple thing
that would increase the general utility of the set class. I suppose I
could start another thread like "feature request: New method for set -
get_equivalent".

AdamB

3/8/2008 4:28:00 PM

0

On Mar 8, 7:32 am, Alan Isaac <ais...@american.edu> wrote:
> Cruxic wrote:
> > people = set( [Person(1, 'Joe'), Person(2, 'Sue')] )
> > ...
> > p = people.get_equivalent(2) #method doesn't exist as far as I know
> > print p.name #prints Sue
>
> def get_equivalent(test, container):
>
> for p in container:
>
> if p == test:
>
> return p
>
> hth,
>
> Alan Isaac
>
> #example (note change in __eq__ to match your case; fix if nec)
>
> class Person:
>
> def __init__(self, id, name):
>
> self.id = id
>
> self.name = name
>
> def __hash__(self):
>
> return self.id
>
> def __eq__(self, other):
>
> return self.id == other
>
> people = set( [Person(1, 'Joe'), Person(2, 'Sue')] )
>
> get_equivalent(2,people)

That works fine for small data sets but my goal is to avoid a linear
search, instead leveraging the O(1) lookup time for a hash based set.

Raymond Hettinger

3/8/2008 7:15:00 PM

0

> > > Is it possible to get an object out of a set() given another object
> > > that has the same hash code and equality (__hash__() and __eq__()
> > > return the same)?
>
> > Yes, but it requires an indirect approach.http://aspn.activestate.com/ASPN/Cookbook/Python/Rec...
>
> > Raymond
>
> That's a clever work around.  Thanks, Raymond.  Clearly you had a need
> for this.  Do you feel it's a common need that should be submitted as
> a Python feature request?  To me it seems like such a simple thing
> that would increase the general utility of the set class.  I suppose I
> could start another thread like "feature request: New method for set -
> get_equivalent".

Glad you liked the recipe. :-) FWIW, it is not specific to sets. The
recipe works with any container including dictionaries and lists. The
approach is easily extended to any situation with equality testing.
For example, it can be used with list.remove(x) to find the identity
of the removed object.

Long ago, I rejected adding get_equivalent() to the set API. The
existing API has a near zero learning curve and it would be nice to
keep it that way.

For most use cases, the obvious, explicit approach is better. Just
make a dictionary where the value is the canonical representative of
the equivalence class:

>>> d = {1:1, 2:2, 3:3}
>>> d[2.0]
2

The intern() builtin uses this approach:

interned = {}
def intern(s):
if s in interned:
return interned[s]
interned[s] = s
return s

Raymond

Aaron Brady

3/8/2008 8:27:00 PM

0

> something something equivalence class.

> The intern() builtin uses this approach:
>
>    interned = {}
>    def intern(s):
>         if s in interned:
>             return interned[s]
>         interned[s] = s
>         return s

If you've seen it before, and have the old one, return the old one.
Do I have this straight?

Raymond Hettinger

3/9/2008 5:37:00 PM

0

> > The intern() builtin uses this approach:
>
> >    interned = {}
> >    def intern(s):
> >         if s in interned:
> >             return interned[s]
> >         interned[s] = s
> >         return s
>
> If you've seen it before, and have the old one, return the old one.
> Do I have this straight?

Right.