Этот код полностью списан из книги, но выполнить я его не могу из-за ошибки на строке 42 типа: unsupported operand type(s) for +: 'int' and 'list'.
В чем проблема?
graph = {}
graph['start'] = {}
graph['start']['A'] = [6]
graph['start']['B'] = [2]
graph['A'] = {}
graph['A']['fin'] = [1]
graph['B'] = {}
graph['B']['A'] = [3]
graph['B']['fin'] = [5]
graph['fin'] = {}
infinity = float('inf')
costs = {}
costs['A'] = 6
costs['B'] = 2
costs['fin'] = infinity
parenst = {}
parenst['A'] = 'start'
parenst['B'] = 'start'
parenst['fin'] = None
processed = []
def find_lowest_cost_node(costs): # определяет узел с наименьшей стоимостью
lowest_cost = float('inf')
lowest_cost_node = None
for node in costs:
cost = costs[node]
if cost < lowest_cost and node not in processed:
lowest_cost = cost
lowest_cost_node = node
return lowest_cost_node
node = find_lowest_cost_node(costs)
while node is not None:
cost = costs[node]
neighbors = graph[node]
for n in neighbors.keys():
new_cost = cost + neighbors[n] # строка 42
if costs[n] > new_cost:
costs[n] = new_cost
parenst[n] = node
processed.append(node)
node = find_lowest_cost_node(costs)