pythonjsondictionarynetworkxfloyd-warshall

Print dictionary elements as a list of paths


I am trying to print all possible paths using Floyd-Warshall algorithm and networkx (Python). The thing is, I don't know how to do it properly (readable, as a list). I have these paths from my graph:

X = nx.floyd_warshall(gra)
Y = {a: dict(b) for a, b in X.items()}
print(Y)

and it returns this:

{(0, 0): {(0, 0): 0, (1, 0): 1, (0, 1): 1, (1, 1): 2, (0, 2): 2, (1, 2): 3}, (1, 0): {(1, 0): 0, (0, 0): 1, (1, 1): 1, (0, 1): 2, (0, 2): 3, (1, 2): 2}, (0, 1): {(0, 1): 0, (0, 0): 1, (1, 1): 1, (0, 2): 1, (1, 0): 2, (1, 2): 2}, (1, 1): {(1, 1): 0, (1, 0): 1, (0, 1): 1, (1, 2): 1, (0, 0): 2, (0, 2): 2}, (0, 2): {(0, 2): 0, (0, 1): 1, (1, 2): 1, (0, 0): 2, (1, 0): 3, (1, 1): 2}, (1, 2): {(1, 2): 0, (1, 1): 1, (0, 2): 1, (0, 0): 3, (1, 0): 2, (0, 1): 2}}

How can I convert it in a more readable format? For example, is it possible to print all possible paths one by one? I would like an output like this:

[(0, 0), (1, 0)]
[(1, 0), (1, 1)]
[(0, 0), (1, 0), (1, 1)]
...
[(0, 0), (1, 0), (1, 1), (0, 1), (0, 2), (1, 2)]

Alternatively, is it possible to print them as a JSON (or like this):

{(0, 0): 
   {(0, 0): 0, 
    (1, 0): 1, 
    (0, 1): 1, 
    (1, 1): 2, 
    (0, 2): 2, 
    (1, 2): 3},  
(1, 0): 
   {(1, 0): 0, 
    (0, 0): 1, 
    (1, 1): 1, 
    (0, 1): 2, 
    (0, 2): 3, 
    (1, 2): 2}, 

[......]

    (0, 1): 2}}

Thanks...


Solution

  • For printing Json:

    import json
    
    
    a = {(0, 0): {(0, 0): 0, (1, 0): 1, (0, 1): 1, (1, 1): 2, (0, 2): 2, (1, 2): 3}, (1, 0): {(1, 0): 0, (0, 0): 1, (1, 1): 1, (0, 1): 2, (0, 2): 3, (1, 2): 2}, (0, 1): {(0, 1): 0, (0, 0): 1, (1, 1): 1, (0, 2): 1, (1, 0): 2, (1, 2): 2}, (1, 1): {(1, 1): 0, (1, 0): 1, (0, 1): 1, (1, 2): 1, (0, 0): 2, (0, 2): 2}, (0, 2): {(0, 2): 0, (0, 1): 1, (1, 2): 1, (0, 0): 2, (1, 0): 3, (1, 1): 2}, (1, 2): {(1, 2): 0, (1, 1): 1, (0, 2): 1, (0, 0): 3, (1, 0): 2, (0, 1): 2}}
    
    
    def dict_key_convert(dic):
        converted = {}
        for key, item in dic.items():
            if isinstance(item, dict):
                sub_dict = dict_key_convert(item)
                converted[str(key)] = sub_dict
            else:
                converted[str(key)] = item
    
        return converted
    
    
    # print(json.dumps(dict_key_convert(a), indent=2))
    with open('temp.json', 'w') as file:
        json.dump(dict_key_convert(a), file, indent=2)
    

    Results:

    {
      "(0, 0)": {
        "(0, 0)": 0,
        "(1, 0)": 1,
        "(0, 1)": 1,
        "(1, 1)": 2,
        "(0, 2)": 2,
        "(1, 2)": 3
      },
      "(1, 0)": {
        "(1, 0)": 0,
        "(0, 0)": 1,
        "(1, 1)": 1,
        "(0, 1)": 2,
        "(0, 2)": 3,
        "(1, 2)": 2
      },
      "(0, 1)": {
        "(0, 1)": 0,
        "(0, 0)": 1,
        "(1, 1)": 1,
        "(0, 2)": 1,
        "(1, 0)": 2,
        "(1, 2)": 2
      },
      "(1, 1)": {
        "(1, 1)": 0,
        "(1, 0)": 1,
        "(0, 1)": 1,
        "(1, 2)": 1,
        "(0, 0)": 2,
        "(0, 2)": 2
      },
      "(0, 2)": {
        "(0, 2)": 0,
        "(0, 1)": 1,
        "(1, 2)": 1,
        "(0, 0)": 2,
        "(1, 0)": 3,
        "(1, 1)": 2
      },
      "(1, 2)": {
        "(1, 2)": 0,
        "(1, 1)": 1,
        "(0, 2)": 1,
        "(0, 0)": 3,
        "(1, 0)": 2,
        "(0, 1)": 2
      }
    }