如何修复__delitem__方法

时间:2016-11-17 06:18:13

标签: 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 __delitem__(self,item):
        if type(item) == tuple:
            for key,value in self.edges.items():
                if key == item[0] and item[1] in value[0]:
                    self.edges[item[0]].remove(item(1))
        elif item in self.edges.keys():
            del self.edges[item]
            for value in self.edges.values():
                if item in value:
                    value.remove(item)
        raise GraphError

delitem需要一个元组或一个str:

如果item参数是2元组,指定两者都在Graph中的原始节点和目标节点,则从Graph中删除该边。如果存在边缘,则更新原始节点和目标节点的集合以反映边缘已被删除。

例如: 呼叫del g [' a',' e']将不会做任何事情,但是呼叫del g [' a'' c']将将图表更改为:

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

如果item参数是Graph中的关键节点 首先从作为原始节点或目标节点的每个集合中删除该节点。 第二,将该节点从边缘删除为密钥。

调用del g [' x']将不执行任何操作(没有节点x),但调用del g [' a']会将图形更改为:

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

但是当我运行delitem函数时,会产生以下错误:

119 # Test __delitem__
122 *Error: del g['a','c'] raised exception TypeError: 'tuple' object is not callable
123 *Error: Failed g.edges == {'a': [{'d'},{'d'}], 'b': [{'c','d'}, set()], 'c': [set(),{'b','d'}], 'd': [{'a','c'},{'a','b'}], 'e': [set(),set()]}
      evaluated: {'b': [{'c', 'd'}, set()], 'e': [set(), set()], 'c': [set(), {'b', 'a', 'd'}], 'a': [{'c', 'd'}, {'d'}], 'd': [{'c', 'a'}, {'b', 'a'}]} == {'b': [{'c', 'd'}, set()], 'e': [set(), set()], 'c': [set(), {'b', 'd'}], 'a': [{'d'}, {'d'}], 'd': [{'c', 'a'}, {'b', 'a'}]}
124 *Error: del g['b','d'] raised exception TypeError: 'tuple' object is not callable
125 *Error: Failed g.edges == {'a': [{'d'},{'d'}], 'b': [{'c'}, set()], 'c': [set(),{'b','d'}], 'd': [{'a','c'},{'a'}], 'e': [set(),set()]}
      evaluated: {'b': [{'c', 'd'}, set()], 'e': [set(), set()], 'c': [set(), {'b', 'a', 'd'}], 'a': [{'c', 'd'}, {'d'}], 'd': [{'c', 'a'}, {'b', 'a'}]} == {'b': [{'c'}, set()], 'e': [set(), set()], 'c': [set(), {'b', 'd'}], 'a': [{'d'}, {'d'}], 'd': [{'c', 'a'}, {'a'}]}
127 *Error: del g['c'] raised exception graph.GraphError: 
128 *Error: Failed g.edges == {'a': [{'d'},{'d'}], 'b': [{'d'}, set()], 'd': [{'a'},{'a','b'}], 'e': [set(),set()]}
      evaluated: {'b': [{'c', 'd'}, set()], 'e': [set(), set()], 'a': [{'c', 'd'}, {'d'}], 'd': [{'c', 'a'}, {'b', 'a'}]} == {'b': [{'d'}, set()], 'e': [set(), set()], 'a': [{'d'}, {'d'}], 'd': [{'a'}, {'b', 'a'}]}
129 *Error: del g['a'] raised exception graph.GraphError: 
130 *Error: Failed g.edges == {'b': [{'d'}, set()], 'd': [set(),{'b'}], 'e': [set(),set()]}
      evaluated: {'b': [{'c', 'd'}, set()], 'e': [set(), set()], 'd': [{'c', 'a'}, {'b', 'a'}]} == {'b': [{'d'}, set()], 'e': [set(), set()], 'd': [set(), {'b'}]}

我不确定有什么问题,有人可以帮我修复我的delitem功能吗?非常感谢。

1 个答案:

答案 0 :(得分:0)

我写了并测试了这个,它似乎适合你的情况!您遇到了一些问题,例如尝试从集合列表中删除项目而不是集合本身,并且每次调用该方法时都会引发GraphError(因为它不在其他地方)

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

    def __delitem__(self,item):
        if type(item) == tuple:
            src, dest = item
            if src in self.edges: # This concisely checks if key in dict
                self.edges[src][0].remove(dest) # note 1
                self.edges[dest][1].remove(src) # note 1
        elif item in self.edges:
            del self.edges[item]
            for value in self.edges.values():
               if item in value[0]:
                   value[0].remove(item)
               if item in value[1]:
                   value[1].remove(item)
        else:
            raise GraphError


#################
#### Tests:  ####
#################
g=Graph()
g.edges = \
 {'a': [{'d'},      {'d'}    ],
  'b': [{'c','d'},  set()    ],
  'c': [set(),      {'b','d'}],
  'd': [{'a','c'},  {'a','b'}],
  'e': [set(),      set()    ]}

del g['a']

from pprint import pprint # For pretty printing
pprint(g.edges)

del g[('d','c')]
pprint(g.edges)