Am new to Python; trying to learn the concept of dictionary while building / operating a network graph. Looks like I am not getting it right.
I have a CSV file of following format:
RC11,RC12,RC13,RC14,RC15,RC16,RC17,RC18,RC19,RC110 RC21,RC22,RC23,RC24,RC25,RC26,RC27,RC28,RC29,RC210 RC31,RC32,RC33,RC34,RC35,RC36,RC37,RC38,RC39,RC310
I read this file to create a network MultiDiGraph using networkx package, where the first two columns represents the two nodes of an edge and the remaining columns constitues various edge attributes that is stored in an attribute dictionary. Using these values I create an entry into a previously initialized nested dictionary, named 'moves'.
# create an empty nested dictionary
movesTree = lambda: defaultdict(movesTree)
moves = movesTree() # based on a suggested solution [here][1]
# read the CSV file using pandas (as pd) package's read_csv()
columns = list(pd.read_csv(fileURI, sep=',', nrows=0).columns) # just headers
routingData = pd.read_csv(fileURI, sep=',').dropna() # entire file
# add edge to the previously created MultiDiGraph routingNetwork
for i, elrow in routingData.iterrows():
routingNetwork.add_edge(elrow[0], elrow[1], attr_dict=elrow[2:].to_dict())
# add values from the row to the dictionary moves
# first 6 values in the row forms the unique key
# last 5 values correspond to various attributes for that key (from header list)
moves[elrow[0]][elrow[1]][elrow[2]][elrow[3]][elrow[4]][elrow[5]] = [(columns[6], elrow[6]), (columns[7], elrow[7]), (columns[8], elrow[8]), (columns[9], elrow[9]), (columns[10], elrow[10])]
print(list(moves))
Unfortunately, the print() above only output the deduped list of the nodes, minus the terminal node. What am I doing wrong?
NOTE: in reality, at each node the moves will need to hold the data for only the out-edges of that node. Before I go to that logic, I was testing the above statement syntax and output and got stuck
I figured out the mistake(s). Changing the last two lines as follows, gives me the desired output. The mistake was in the improper way of writing the composite key to the dictionary "moves."