我需要在 python 元组中做一个集合差异,但是差异需要考虑我的元组的第一个元素。
为了实现这一点,我(不成功地)使用了此类方法
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
在 sets.py 模块中挖掘我发现该库使用 itertools.ifilterfalse 函数来产生差异
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
但是我无法对此做任何有用的事情。