I need to create something like this to represent a directed weighted graph based on user input -
graph = {
'a': {'b': 1, 'c': 4},
'b': {'c': 3, 'd': 2, 'e': 2},
'c': {},
'd': {'b': 1, 'c': 5},
'e': {'d': -2}
}
So far,
import pprint
graph = {}
values = {}
v = int(input("Enter number of vertices: "))
print("Enter vertices(keys) : ")
for i in range(v):
graph.setdefault(input())
edges = {}
for x in graph:
edges.setdefault(x)
for i in graph:
graph[i] = edges
print("Enter weights: ")
for i in graph:
print(i)
for j in graph[i]:
var = input()
graph[i][j] = var
pprint.pprint(graph)
I tried but for some reason, it is replacing the previously read weights with last read weights. Any solutions?