3
I am implementing a Python code for Graph creation
with open (filename, "r") as f:
d = {}
for line in f:
key, value = line.split()
if key not in d:
d[key] = [value]
else:
d[key].append(value)
However my algorithm is returning the string dictionary and not whole, look at my output:
{'1': ['2', '4', '5'], '0': ['1'], '3': ['2', '7'], '2': ['3 ',' 6 '],' 5 ': [' 6 '],' 4 ': [' 5 ',' 0 '],' 7 ': []' 6 ': [' 5 ',' 7 ']}
how do I convert the whole key and value or get everything as an integer?
There is some error or just the output is not as expected?
– Leonel Sanches da Silva
only the output should generate a Graph with integers and not string.
– Jonnathan Lopes
Do you want both the key and the value as integers? Or just the values? (if you want the key too, use
int
also inkey
, just be careful to do that before you test your presence ondict
, not after)– mgibsonbr