Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python集().IsubSet()未按预期工作_Python_Python 2.7_Set - Fatal编程技术网

Python集().IsubSet()未按预期工作

Python集().IsubSet()未按预期工作,python,python-2.7,set,Python,Python 2.7,Set,我试图使用set().issubset()来比较序列。正如你所能想象的,它没有按预期工作;)预告:抱歉,代码太长了 class T(object): def __init__(self, value, attributes = None): self.value = value self.attributes = Attributes(attributes) def __eq__(self, other): if not isinstance(other, T)

我试图使用
set().issubset()
来比较序列。正如你所能想象的,它没有按预期工作;)预告:抱歉,代码太长了

class T(object):
  def __init__(self, value, attributes = None):
    self.value = value
    self.attributes = Attributes(attributes)

  def __eq__(self, other):
    if not isinstance(other, T):
      return False
    if self.value == other.value and self.attributes == other.attributes:
      return True
    else:
      return False

  def __ne__(self, other):
    if not isinstance(other, T):
      return True
    if self.value != other.value or self.attributes != other.attributes:
      return True
    else:
      return False

class Attributes(dict):
  def __init__(self, attributes):
    super(dict, self)
    self.update(attributes or dict())

  def __eq__(self, other):
    if self.items() == other.items():
      return True
    else:
      return False

  def __ne__(self, other):
    if not self.items() == other.items():
      return True
    else:
      return False

  def __cmp__(self, other):
    return self.items().__cmp__(other.items())


x = [T("I", {'pos': 0}), T("am", {'pos': 1}), T("a", {'pos': 2}), T("test", {'pos': 3})]
y = [T("a", {'pos': 2}), T("test", {'pos': 3})] 
xx = set(x)
yy = set(y)

assert y[0] == x[2], "__eq__ did fail, really?" #works
assert y[1] == x[3], "__eq__ did fail, really?" #works
assert xx-(yy-xx) == xx, "set subtract not working" #works, but is nonsense. see accepted answer
assert not xx.issubset(yy), "i am doing it wrong..." #works
assert yy.issubset(xx), "issubset not working :(" #FAILS!
运行上述代码会导致最后一次断言失败:

$ python
Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import issubsettest
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "issubsettest.py", line 52, in <module>
    assert yy.issubset(xx), "issubset not working :("
AssertionError: issubset not working :(
>>> 
$python
Python 2.7.2(v2.7.2:8527414a2,2011年6月11日,15:22:34)
[GCC 4.2.1(苹果公司建造5666)(dot 3)]关于达尔文
有关详细信息,请键入“帮助”、“版权”、“信用证”或“许可证”。
>>>进口签证
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“issubsettest.py”,第52行,在
断言yy.issubset(xx),“issubset不工作:(”
AssertionError:issubset不工作:(
>>> 

我在这里遗漏了什么?

您的对象正在被其
id
散列(您没有覆盖
\uuuuuuhash\uuuuuu
)。当然,它们不是子集,因为
xx
yy
包含唯一的对象

为了做到这一点,您需要使用某种
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
函数

class T(object):
    #<snip> ...

    def __hash__(self):
        return hash(self.value)

    #... </snip>

这应该很明显为什么这是
真的

我认为这是因为用户我认为问题在于你的类
T
没有覆盖方法
\uuuuuuuhash\uuuuuu
,其实例在散列集合中不可用。我刚刚用Python 3.3测试了您的代码,它给出了错误:
TypeError:unhabable type:'T'
xx=set(x)行上
所以肯定是缺少
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
。然后解释他应该如何修复它。@JBernardo——修复它完全是另一回事。OP需要以某种方式覆盖
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu…这有帮助吗?好的,我明白了,谢谢!让我困惑的是,
xx-(yy-xx)
似乎起到了一些作用。但我认为它所做的仅仅是
yy-set()
括号内和
xx-set()
之后。事后看来,这个断言甚至是完全错误的。我认为我无法通过减法证明任何东西。@mgilson是的!谢谢你的解释!
xx - set([]) == xx