m0_64677750 2023-01-05 16:18 采纳率: 50%
浏览 13
已结题

关于#File#的问题,如何解决?


class Graph:
            def __init__(self,filename):#将地图建成一个邻接表
        
               graph_edges=[]#边的长度
               with open(filename) as fhandle:#读取文件,一行一行的读
                      for line in fhandle:
                          if line=="\n":#读取截止条件,注意必须加否则会报错
                            break
                #将map.txt文件中的数据按空格分离并存储,*_代表这一行后面所有的元素。
                          edge_from,edge_to,cost,*_=line.strip().split(" ")
                          graph_edges.append((edge_from,edge_to,cost))#以元组的形式加入到graph_edges
        #建立节点,set() 函数创建一个无序不重复元素集,
        #可进行关系测试,删除重复数据,还可以计算交集、差集、并集等。
               self.nodes =set()
               for edge in graph_edges:
            #初始化节点
            #update() 方法用于修改当前集合,可以添加新的元素或集合到当前集合中,
            #如果添加的元素在集合中已存在,则该元素只会出现一次,重复的会忽略。
                  self.nodes.update([edge[0],edge[1]])
        #建立邻接表
               self.adjacency_list = {node: set() for node in self.nodes}
               for edge in graph_edges:
           #字典中的键表示图中的节点,而键值则是以字典的形式存在里面包括几组一元组的形式储存的
           #表示可达到节点以及权值
                   self.adjacency_list[edge[0]].add((edge[1],edge[2]))
            def tsp(self, start, end):
                   # 初始化未遍历的点的集合
                      unvisited = set(self.nodes)
    # 将起始点加入到已遍历的点的集合中
                      visited = {start}
    # 初始化当前点为起始点
                      current = start
    #初始化路径和距离为 0
                      path = []
                      distance = 0
                      while unvisited:
    # 寻找当前点的最近的未遍历的点
                             next_node, next_distance = min([(node, cost) for node, cost in self.adjacency_list[current] if node not in visited], key=lambda x: x[1])
    # 将最近的未遍历的点加入到已遍历的点的集合中
                             visited.add(next_node)
    # 从未遍历的点的集合中删除最近的未遍历的点
                             unvisited.remove(next_node)
    # 更新当前点为最近的未遍历的点
                             current = next_node
    # 更新路径和距离
                             path.append(next_node)
                             distance += next_distance

# 如果最后一个遍历的点不是终点,添加一条从最后一个遍历的点到终点的边
                             if current != end:
    # 寻找最后一个遍历的点到终点的最短距离
                                 next_distance = min([cost for node, cost in self.adjacency_list[current] if node == end])
    # 更新路径和距离
                                 path.append(end)
                                 distance += next_distance

                      return path, distance


graph = Graph('C:/Users/张心仪/Desktop/map.txt')
path, distance = graph.tsp('A', 'B')
print(path)  # ['A', 'B', 'C', 'D']
print(distance)  # 8

File "C:\Users\张心仪\Desktop\untitled34.py", line 61, in
path, distance = graph.tsp('A', 'B')

File "C:\Users\张心仪\Desktop\untitled34.py", line 47, in tsp
distance += next_distance

TypeError: unsupported operand type(s) for +=: 'int' and 'str'
怎么解决这样的报错

  • 写回答

1条回答 默认 最新

  • 於黾 2023-01-05 16:33
    关注

    distance 和next_distance类型不一致

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 1月18日
  • 已采纳回答 1月10日
  • 创建了问题 1月5日

悬赏问题

  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题