我想在当前图形中添加边这是我的图形代码def mGraph(): graph = {'A': {'B': 1, 'C': 2}, 'B': {'C': 2, 'D': 1}, 'C': {'D': 1}, 'D': {'C': 2}, 'E': {'F': 1}, 'F': {'C': 2}} return graph这是添加边功能def add_edge(graph, aa1, aa2): graph[aa1].update()[aa2] graph[aa2].update()[aa1] return graph我要求用户输入第一个节点和第二个节点def main(): graph = mGraph() option = '' while option != 'terminate': print(" a. Display") print(" c. Add Edges") option = input("Enter your option:") if option == "a": print(graph) if option == "c": a1 = input("First Edge") a2 = input("Second Edge") print(add_edge(graph, a1, a2))main()我想将两条边链接到当前图形并想修改它们。如果用户在“First Edge”中输入 A,在“Second Edge”中输入 D。我想看到图表中的变化。用户输入应该反映在图表上。我有这样的错误Traceback (most recent call last): File "C:......", line 52, in <module> main() File "C:......", line 49, in main print(add_edge(graph, a1, a2)) File "C:......", line 25, in add_edge graph[aa1].update()[aa2]TypeError: 'NoneType' object is not subscriptable
1 回答
杨魅力
TA贡献1811条经验 获得超6个赞
试试这个 :
def add_edge(graph, aa1, aa2):
first_value = graph[aa1].copy()
for k,v in first_value.items():
if aa2 in k:
graph[aa1].update({aa2:v+1})
break
else:
graph[aa1].update({aa2:1})
second_value = graph[aa2].copy()
for k,v in second_value.items():
if aa1 in k:
graph[aa2].update({aa1:v+1})
break
else:
graph[aa2].update({aa1:1})
return graph
- 1 回答
- 0 关注
- 100 浏览
添加回答
举报
0/150
提交
取消