I need to do a set difference in python tuples, but the difference need to consider the first element of my tuple.
To achieve this I did (uncessfully) using this class approach
class Filedata(object):
def __init__(self, filename, path):
self.filename = filename
self.path = path + '\\' + filename
def __eq__(self, other):
return self.filename==other.filename
def __ne__(self, other):
return self.filename!=other.filename
def __call__(self):
return self.filename
def __repr__(self):
return self.filename
Digging in the sets.py module I found that the library uses the itertools.ifilterfalse function to make the difference
def difference(self, other):
"""Return the difference of two sets as a new Set.
(I.e. all elements that are in this set and not in the other.)
"""
result = self.__class__()
data = result._data
try:
otherdata = other._data
except AttributeError:
otherdata = Set(other)._data
value = True
for elt in ifilterfalse(otherdata.__contains__, self):
data[elt] = value
return result
But I wasn’t able to do anything useful with this.
The only way to do this is to define your own sequence class that only uses the first element in
__eq__()and__hash__().