Dictionary Creation of Graph Returning integers

3

I'm implementing a Python code to create Graph

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)

But my Algorithm is returning the dictionary in string and not integer, 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 to integer the key and value or get everything as integer?

    
asked by anonymous 07.12.2015 / 23:20

1 answer

3

You are reading a file. Natural that are strings .

To type for integer, modify your code to the following:

with open (filename, "r") as f:
    d = {}
    for line in f:
        key, value = line.split()
        if key not in d:
            d[key] = [int(value)]
        else:
            d[key].append(int(value))
    
07.12.2015 / 23:41