From: Alex Martelli [mailto:al*****@yahoo.com] Roman Yakovenko <ro*************@actimize.com> wrote: Thanks. I have an other question, more general: I have class A that defines __eq__ and __ne__. I have 2 lists of instances of A What is the right way to find out whether those lists are equal (as sets) or not? Thanks for help If instances of class A are not hashable, there is unfortunately no fast way. Tim Peters, in the Python Cookbook (first edition), shows an elaborate way to turn a list into a list of unique elements which is as fast as feasible depending on the list elements' properties, which the recipe discovers automatically by fielding errors raised by usage that the list items don't support -- but even that would be thrown off the rails if the instances of A _appear_ to be hashable but violate the key semantic constraint (equality of instance MUST imply equality of hash). I assume from your specific mention of __eq__ and __ne__ that you can't even SORT a list of such instances -- that they don't define __lt__ or define it in such ways as to violate the key semantic constraint on THAT operation -- so you can't even use the second-best approach (after hashing), which starts with sorting.
Right, this is exactly my constraints:
classes have __eq__, __ne__. Classes are mutable - I can't define
__hash__ function. __lt__ - I can implement but it will be meaningless.
Under such dire, horrible conditions you will have to resort to the extremely slow approach, O(M*N) where M and N are the lengths of the two lists -- at least it can be expressed simply...: def same_as_sets(onelist, another): for item in onelist: if item in another: return False for item in another: if item in onelist: return False return True It's horrible, but then that class appears to be designed to fight against attempts to solve this problem more smartly, at least extrapolating from what little you tell us about it. Alex
I agree with you: it's horrible.
Thank you for help. I think I have a dicision:
1. I will implement meaningless __lt__
2. I will sort ( I don't have duplicated items ) every time I need to compare
2.1 Because sort is happen in place next time it will take less time to sort.
Again - Thanks for help. It was very usefull. It seems that I had wrong expectation
from set - " unordered set collection based only on comparison operators".
My mistake.
Roman 1 1602
Roman Yakovenko <ro*************@actimize.com> wrote:
... classes have __eq__, __ne__. Classes are mutable - I can't define __hash__ function. __lt__ - I can implement but it will be meaningless.
As long as it respects the fundamental semantics constraints such as:
a < b and b < c imply a < c
a < b implies not (a == b)
a < b implies (a != b)
not (a < a) for any a
and so on, your __lt__will not be 'meaningless' but very useful.
Basically, __lt__ is meaningful if it's transitive, non-reflective, and
compatible with your == and != (which I assume are compatible with each
other); a transitive non-reflective < defines implicitly an equivalence
relation, a eqv b <--> not (a < b or b < a), and you need your == to
express exactly that equivalence relation... so if your == is
meaningful, your < can't really be 'meaningless'!-)
Thank you for help. I think I have a dicision: 1. I will implement meaningless __lt__ 2. I will sort ( I don't have duplicated items ) every time I need to compare 2.1 Because sort is happen in place next time it will take less time to sort.
Yes, that does seem to make sense to me. Once two lists without
duplicates are sorted, they're equal as sets iff they're == as lists;
and yes, maintaining sorted order is typically quite cheap in Python due
to Tim Peters' powerful natural mergesort algorithm as implemented
inside list.sort (though it might be worth having a look at the bisect
module, it's likely going to be faster to just sort the lists each
time).
Again - Thanks for help. It was very usefull. It seems that I had wrong
expectation from set - " unordered set collection based only on comparison operators". My mistake.
Ah, isn't set documented to need hashable elements? It should be. Of
course, _why_ your class appears to be hashable when it defines __eq__
and not __hash__ I dunno -- Python should diagnose that, but it does't
appear to...
Alex This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: democratix |
last post by:
Hi,
I've only got a couple years experience developing for Access but have
recently been experimenting with HTML/javascript for gui and
client-side scripting, mysql for database and php for...
|
by: Roman Yakovenko |
last post by:
Hi. Could somebody explain why sets and dict are different ?
from sets import Set as set
class A(object):
def __init__( self, x ):
self.x = x
def __eq__( self, other ):
return self.__dict__...
|
by: toocool |
last post by:
Hi, I'm having a problem with one query. My database has the following
fields: id(int), time(int), and groupfield(enum - 5 possibilities). I
want to select all the rows where the time is between...
|
by: Dino |
last post by:
I am creating a website that is going to ask the user to enter a date! then
from an access database get all records where a date field is greater then
the date entered! Sounds simple, I do it in...
|
by: Steven D'Aprano |
last post by:
Playing around with comparisons of functions (don't ask), I discovered an
interesting bit of unintuitive behaviour:
>>> (lambda y: y) < (lambda y: y)
False
Do the comparison again and things...
| |
by: mayamorning123 |
last post by:
A comparison among six VSS remote tools including SourceOffSite ,
SourceAnyWhere, VSS Connect, SourceXT, VSS Remoting, VSS.NET
To view the full article, please visit...
|
by: spam.noam |
last post by:
Hello,
Guido has decided, in python-dev, that in Py3K the id-based order
comparisons will be dropped. This means that, for example, "{} < "
will raise a TypeError instead of the current...
|
by: michael.f.ellis |
last post by:
The following script puzzles me. It creates two nested lists that
compare identically. After identical element assignments, the lists
are different. In one case, a single element is replaced. In...
|
by: Bill Jackson |
last post by:
For example,
class A:
def __init__(self,a):
self.a = a
def __eq__(self, other):
return self.a == other.a
class B:
def __init__(self,b):
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |