My attempts: I have created a DiGraph using networkx package and two edges were added. Then tried to obtain adjacency list using G.adj
. Then I received the adjacency list in the type of AdjacencyView. I have tried changing the output by list(G.adj)
but it did not give me the expected result.
G =nx.DiGraph()
G.add_edges_from([('1','2'),('2','3')])
G.adj
Out[65]: AdjacencyView({'1': {'2': {}}, '2': {'3': {}}, '3': {}})
list(G.adj)
Out[66]: ['1', '2', '3']
Then I tried using dict(G.adj)
then I received the expected output partially but type AtlasView was visible.
dict(G.adj)
Out[70]: {'1': AtlasView({'2': {}}), '2': AtlasView({'3': {}}), '3': AtlasView({})}
What I'm looking for: I'm looking for just to output the adjacency list as a type dict as shown below. May I know how to convert AdjacencyView type output to a type of dict ? Thank you!
{'1': {'2': {}}, '2': {'3': {}}, '3': {}}
Out[71]: {'1': {'2': {}}, '2': {'3': {}}, '3': {}}
Try adjacency:
G = nx.DiGraph()
G.add_edges_from([('1','2'),('2','3')])
print({k: v for k, v in G.adjacency()})
Output:
{'1': {'2': {}}, '2': {'3': {}}, '3': {}}