Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.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的“in”和“subset”方法工作?_Python_Class_Set - Fatal编程技术网

如何使python的“in”和“subset”方法工作?

如何使python的“in”和“subset”方法工作?,python,class,set,Python,Class,Set,我对python比较陌生。我有一个类时间,我想检查一组时间对象是否包含另一组时间对象 a = {Time(10,10)} print {Time(10,10)}.issubset(a) >> "result is False" for i in a: print i in a >> "result is True" 在课堂上,我实现了这些方法 def to_min(self): return self.h * 60 + self.m def __cmp

我对python比较陌生。我有一个类时间,我想检查一组时间对象是否包含另一组时间对象

a = {Time(10,10)} 
print {Time(10,10)}.issubset(a) >> "result is False"
for i in a:
    print i in a >> "result is True"
在课堂上,我实现了这些方法

def to_min(self):
    return self.h * 60 + self.m
def __cmp__(self, other):
    if isinstance(other, Time):
        if self.to_min() > other.to_min():
            return 1
        else:
            if self.to_min() == other.to_min():
                return 0
            else:
                return -1
def __eq__(self, other):
    if isinstance(other, Time):
        if self.to_min() == other.to_min():
            return True
        else:
            return False
def __gt__(self, other):
    return self.to_min() > other.to_min()
def __ge__(self, other):
    return self.to_min() >= other.to_min()
def __lt__(self, other):
    return self.to_min() < other.to_min()
def __le__(self, other):
    return self.to_min() <= other.to_min()
def __str__ (self):
    return str(self.h) + ":" + str(self.m)
def __hash__(self):
    return self.to_min()
我换了这个

 self.to_min() == other.to_min()
用这个

 self.__hash__() == other.__hash__()
并且还编辑了eq以返回boollean,而不是整数 现在它起作用了,我仍然想知道。 无论如何,如果有人感兴趣,这是完整的代码:

class Time(object):
'''
classdocs
'''


def __init__(self, h, m):
    if isinstance(h, int) and isinstance(h, int):
        self.m = m
        self.h = h
        if(self.m >= 60):
            self.h += self.m // 60
            self.m %= 60
def __add__(self, m):

    return Time(self.h, self.m + m)
def to_min(self):
    return self.h * 60 + self.m
def __cmp__(self, other):
    print "__cmp__"
    if isinstance(other, Time):
        if self.to_min() > other.to_min():
            return 1
        else:
            if self.__hash__() == other.__hash__():
                return 0
            else:
                return -1
def __eq__(self, other):
    print "__eq__"
    if isinstance(other, Time):
        if self.to_min() == other.to_min():
            return True
        else:
            return False
def __gt__(self, other):
    return self.to_min() > other.to_min()
def __ge__(self, other):
    return self.to_min() >= other.to_min()
def __lt__(self, other):
    return self.to_min() < other.to_min()
def __le__(self, other):
    return self.to_min() <= other.to_min()
def __str__ (self):
    return str(self.h) + ":" + str(self.m)
def __hash__(self):
    print "__hash__"
    return self.to_min()
    # return 1
def __ne__(self, other):
    print "__ne__"
    return not self == other



# a = set([Time(10,10), Time(10,20)])
# b = set([Time(10,10)])
# print a in set([b])
a = {Time(10,10)} 
print {Time(10,10)}.issubset(a) 
# print b.issubset( a)
# for i in a:
#     print i in a

你能把时间10,10.0打印出来给我们看结果吗?我想问题出在我的eq上
class Time(object):
'''
classdocs
'''


def __init__(self, h, m):
    if isinstance(h, int) and isinstance(h, int):
        self.m = m
        self.h = h
        if(self.m >= 60):
            self.h += self.m // 60
            self.m %= 60
def __add__(self, m):

    return Time(self.h, self.m + m)
def to_min(self):
    return self.h * 60 + self.m
def __cmp__(self, other):
    print "__cmp__"
    if isinstance(other, Time):
        if self.to_min() > other.to_min():
            return 1
        else:
            if self.__hash__() == other.__hash__():
                return 0
            else:
                return -1
def __eq__(self, other):
    print "__eq__"
    if isinstance(other, Time):
        if self.to_min() == other.to_min():
            return True
        else:
            return False
def __gt__(self, other):
    return self.to_min() > other.to_min()
def __ge__(self, other):
    return self.to_min() >= other.to_min()
def __lt__(self, other):
    return self.to_min() < other.to_min()
def __le__(self, other):
    return self.to_min() <= other.to_min()
def __str__ (self):
    return str(self.h) + ":" + str(self.m)
def __hash__(self):
    print "__hash__"
    return self.to_min()
    # return 1
def __ne__(self, other):
    print "__ne__"
    return not self == other



# a = set([Time(10,10), Time(10,20)])
# b = set([Time(10,10)])
# print a in set([b])
a = {Time(10,10)} 
print {Time(10,10)}.issubset(a) 
# print b.issubset( a)
# for i in a:
#     print i in a