graph in python from a list of edges
Question: I have a list of edges in a text file:

    
    
    0 1
    0 2
    0 3
    1 637
    1 754
    1 1319 
    1 1350
    1 1463
    1 1523
    2 637
    2 754
    2 1319
    2 1350
    2 1463
    2 1523
    3 499
    3 539
    3 595
    3 637
    3 706
    3 1128
    3 1194
    3 1213
    3 1319
    .. ...
    

I need to turn it into a dictionary like this:

    
    
    graph = { "a" : ["c"],
          "b" : ["c", "e"],
          "c" : ["a", "b", "d", "e"],
          "d" : ["c"],
          "e" : ["c", "b"],
          "f" : []
        }
    

my attempt so far has been:

    
    
    import numpy as np
    file_name='/Volumes/City_University/data_mining/Ecoli.txt'
    key_column=0
    
    dat=np.genfromtxt(file_name,dtype=str)
    d={i:[] for i in np.unique(dat[:,key_column])}
    
    for row in dat:
        for key in d.keys():
            if row[key_column]==key :d[key].append(row[1])
    
    print (d)
    

However, this does not work properly inasmuch i don't get a new key when this
appear in the values: as an example I get:

    
    
    '0': ["1", "2", "3"]
    '1': ['637', '754', '1319', '1350', '1463', '1523']
    

in the '1', the "0" is missing.

to make it more simple. If I have a text like this

    
    
    a b
    c d
    

I should get an outcome like: graph = { "a": ["b"], "b": ["a"], "c": ["d"],
"d": ["c"]}

thank you in advance


Answer: If you want a bidirectional graph, you need two appends.

Also, you don't really need the `for key in d.keys()` loop, just append to
`d[row[0]]` instead of `d[key]`.

    
    
    for row in dat:
        d[row[0]].append(row[1])
        d[row[1]].append(row[0])
    

* * *

Also, consider using a defaultdict, in which case you wouldn't need to
initialize `d` with `np.unique`. It will also guard against errors that would
otherwise occur when a node only appears in the second column.

    
    
    import numpy as np
    from collections import defaultdict
    
    file_name='/Volumes/City_University/data_mining/Ecoli.txt'
    dat=np.genfromtxt(file_name,dtype=str)
    d=defaultdict(list)
    
    for row in dat:
        d[row[0]].append(row[1])
        d[row[1]].append(row[0])
    
    print (d)
    

