473,385 Members | 1,546 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

set of sets

I thought rewriting __hash__ should be enough to avoid mutables problem but:

class H(set):
def __hash__(self)
return id(self)

s=H()

f=set()

f.add(s)
f.remove(s)

the add succeeds
the remove fails eventually not calling hash(s).
Thanks for help

Paolino

___________________________________
Yahoo! Mail: gratis 1GB per i messaggi e allegati da 10MB
http://mail.yahoo.it
Aug 11 '05 #1
5 2552
Paolino wrote:
I thought rewriting __hash__ should be enough to avoid mutables problem
but:

class H(set):
def __hash__(self)
return id(self)

s=H()

f=set()

f.add(s)
f.remove(s)

the add succeeds
the remove fails eventually not calling hash(s).


Why don't you just use "frozenset"?

--
Ciao,
Matteo
Aug 11 '05 #2
Matteo Dell'Amico wrote:
Paolino wrote:
I thought rewriting __hash__ should be enough to avoid mutables problem
but:

class H(set):
def __hash__(self)
return id(self)

s=H()

f=set()

f.add(s)
f.remove(s)

the add succeeds
the remove fails eventually not calling hash(s).

Why don't you just use "frozenset"?

This is what I'm doing, but the problem remains IMO.
Anyway with frozenset I have to override __new__ instead of __init__ to
make the initialization which is an operation not described in the
frozenset docs, which makes subclassing frozenset a different operation.

Thanks Paolino
___________________________________
Yahoo! Messenger: chiamate gratuite in tutto il mondo
http://it.beta.messenger.yahoo.com
Aug 11 '05 #3
Matteo Dell'Amico wrote:
Paolino wrote:
I thought rewriting __hash__ should be enough to avoid mutables problem
but:

class H(set):
def __hash__(self)
return id(self)

s=H()

f=set()

f.add(s)
f.remove(s)

the add succeeds
the remove fails eventually not calling hash(s).

Why don't you just use "frozenset"?

And mostly with sets remove operation expense should be sublinear or am
I wrong?
Is this fast as with lists?
Obviously if I use the ids as hash value nothing is guaranted about the
objects contents to be unique but I don't care.
My work is a self organizing net,in which the nodes keep a structure to
link other nodes.As the nature of the net,the links are moved frequently
so remove and add operations and contains query should be optimized.
Why objects need to be hashable for this? Isn't __hash__ there to solve
the problem?
PS Looks like the problem is not present in class sets.Set


___________________________________
Yahoo! Mail: gratis 1GB per i messaggi e allegati da 10MB
http://mail.yahoo.it
Aug 11 '05 #4
Paolino wrote:
Matteo Dell'Amico wrote:

Why don't you just use "frozenset"?


This is what I'm doing, but the problem remains IMO.
Anyway with frozenset I have to override __new__ instead of __init__ to
make the initialization which is an operation not described in the
frozenset docs, which makes subclassing frozenset a different operation.


Don't subclass.

In [1]: s = frozenset()

In [2]: f = set()

In [3]: f.add(s)

In [4]: f
Out[4]: set([frozenset([])])

In [5]: f.remove(s)

In [6]: f
Out[6]: set([])

--
Robert Kern
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Aug 11 '05 #5
Paolo Veronelli wrote:
And mostly with sets remove operation expense should be sublinear or am
I wrong?
Is this fast as with lists?
It's faster then with lists... in sets, as with dicts, remove is on
average O(1).
Obviously if I use the ids as hash value nothing is guaranted about the
objects contents to be unique but I don't care.
My work is a self organizing net,in which the nodes keep a structure to
link other nodes.As the nature of the net,the links are moved frequently
so remove and add operations and contains query should be optimized.
Why objects need to be hashable for this? Isn't __hash__ there to solve
the problem?


The idea of a set of mutable sets looks a bit odd to me...
I don't get why the outer container should be a set, since you don't
care about uniqueness... if you are representing a graph (which seems
the case to me), I'd use an identifier for each node, and a dictionary
mapping node-ids to its adjacency set for each node. For instance,

0 <-- 1 --> 2 --> 3
| |
v v
4 --> 5

would be represented as

{0: set([]), 1: set([0, 2]), 2: set([2,4]), 3: set([5]), 4: set([5]),
5: set([])}

If node ids are consecutive integers, you could also of course use a
list as the outer structure.

PS: we could also discuss this in italian in it.comp.lang.python :)

--
Ciao,
Matteo
Aug 11 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Raymond Hettinger | last post by:
I've gotten lots of feedback on the itertools module but have not heard a peep about the new sets module. * Are you overjoyed/outraged by the choice of | and & as set operators (instead of + and...
5
by: Raymond Hettinger | last post by:
For Py2.4, I'm working on a C implementation of Sets.py with the possibility of having a set() type as a builtin. There is a question as whether the current design for set of sets has been useful....
1
by: Gerrit Holl | last post by:
Brett C. wrote: > Sets now at blazing C speeds! > ----------------------------- > Raymond Hettinger implemented the sets API in C! The new built-ins are > set (which replaces sets.Set) and...
0
by: Dave Benjamin | last post by:
Hola, I made a backport of sets.py that will run on Jython 2.1. Here is a diff against the Python 2.3 version of sets.py. The changes were simple, but I may have made a mistake here or there,...
7
by: Steve | last post by:
This post has two parts. First is my feedback on sets. (Hello? Last summer called, they want their discussion thread back...) Second is some questions about my implementation of a partition...
2
by: mkppk | last post by:
I have kind of strange change I'd like to make to the sets.Set() intersection() method.. Normally, intersection would return items in both s1 and s2 like with something like this: ...
11
by: Prateek | last post by:
I have 3 variable length lists of sets. I need to find the common elements in each list (across sets) really really quickly. Here is some sample code: # Doesn't make sense to union the sets -...
1
by: JosAH | last post by:
Greetings, Introduction This week I'll write a bit about generics (those funny angular brackets). I need an example and decided to use sets and some of their operations. This weeks' article...
6
by: happyhondje | last post by:
Hello everyone, I've got a little issue, both programming and performance-wise. I have a set, containing objects that refer to other sets. For example, in a simple notation: (<a, b, c>, <d, e>)...
17
by: Larry Bates | last post by:
You can do the following: a = del a and a = {1:'1', 2: '2', 3: '3', 4:'4', 5:'5'} del a
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.