Read txt with numbers and create a dictionary in Python

1

Good afternoon I'm trying to create a graph from a Txt file but in the generated dictionary the numbers are coming out as if they were strings someone can help me, it follows the code: File txt

'
0   1
1   2
1   4
1   5
2   3
2   6
3   2
3   7
4   5
4   0
5   6
6   5
6   7'

with open("/Users/jonnathanvinicius/Desktop/Trabalho PAA/cormen.txt","r") as f:    
G={}

    for line in f:
        u, v = line.split()
        if u not in G:
            G[u] = [v]
        else:
            G[u].append(v)
print "O Grafo Web-NotreDame foi Criado"

print output {'1': ['2', '4', '5'], '0': ['1'], '3': ['2', '7'], '2': ['3', '6'], '5': ['6'], '4': ['5', '0'], '6': ['5', '7']}

    
asked by anonymous 07.12.2015 / 17:23

1 answer

1

Just convert the characters to integers.

u, v = map(int, line.split())

Changing this line results in

{0: [1], 1: [2, 4, 5], 2: [3, 6], 3: [2, 7], 4: [5, 0], 5: [6], 6: [5, 7]}
    
11.12.2015 / 02:00