如何修复le方法

时间:2016-11-16 20:38:02

标签: python class

 {'a': [{'c','d'},  {'d'}        ],
 'b': [{'c','d'},  set()        ],
 'c': [set(),      {'a','b','d'}],
 'd': [{'a','c'},  {'a','b'}    ],
 'e': [set(),      set()        ]}

class Graph:
    def __init__(self,*args):
        self.edges = {}

    def __le__(self,right):
        for key, value in self.edges.items():
            if key not in  right.items() or value not in right.items():
                return False
        return True

定义< =运算符以比较两个图。我们定义g1< = g2的含义,因为g1是g2的子图,这意味着 g1中存在的所有节点都存在于g2中 g1中的所有边prsent(原始节点,目标节点)都存在于g2中。 平凡地,对于任何图g,g< = g计算为真。

enter image description here

当我运行我的le函数时,它会产生以下错误:

g1.edges = {'a': [{'c'},{'d'}], 'b': [{'c','d'}, set()], 'c': [set(),{'a','b'}], 'd': [{'a'},{'b'}]}

g2.edges = {'a': [{'c'},{'d'}], 'b': [{'c','d'}, set()], 'c': [set(),{'a','b','d'}], 'd': [{'a','c'},{'b'}]}

g3.edges = {'a': [{'c','d'},{'d'}], 'b': [{'c','d'}, set()], 'c': [set(),{'a','b','d'}], 'd': [{'a','c'},{'a','b'}], 'e': [set(),set()]}


104 # Test <=
111 *Error: g1 <= g1 and g2 <= g2 and g3 <= g3 raised exception; unevaluated: True
112 *Error: g1 <= g2 raised exception; unevaluated: True
113 *Error: g2 <= g3 raised exception; unevaluated: True
115 *Error: g1 <= g2 raised exception; unevaluated: False
117 *Error: g2 <= g3 raised exception; unevaluated: False

有人可以帮我修复我的le功能吗?谢谢。

1 个答案:

答案 0 :(得分:1)

正确 图形对象,因此它没有要调用的项属性。一旦添加了边缘,它就会修复你的代码错误,但它可能无法解决逻辑问题。谢谢。

def __le__(self,right):
   for key, value in self.edges.items():
      if key not in  right.edges.items() or value not in right.edges.items():
         return False
   return True