everyone. I'm writing a simple function in Python 2.7 using the Networkx 1.9 Module for testing that my graph partitioning algorithm implementation works correctly. To that end, I have a list, dvecs, which has a list for each block in the partition of lists which give information about the edges from each node to the classes in the partition. Take a gander:
#dvecs : list of len(P) lists which correspond to a list of degree vectors of each block
numBlocks = len(P)
dvecs = [[]] * numBlocks
for block in P:
blockNo = P.index(block)
dvecs[blockNo] = [[-1] * numBlocks] * len(block)
for node in block:
nodeNo = block.index(node)
for otherBlock in P:
otherBlockNo = P.index(otherBlock)
dvecs[blockNo][nodeNo][otherBlockNo] = len(set(nx.neighbors(G, node)).intersection(set(otherBlock)))
The problem I'm having is that in the last line of the nested loop, the line that starts with dvec[blockNo...], according to the debugger, each of the entries in the middle-depth list(the one with indices specified by nodeNo) are being updated with the same value for each iteration of the innermost loop. In other words, it is as if 'node' is being held constant and nodeNo is iterated through all nodes in the block. What is going on here?
In response to Roman, I tried the following:
for blockNo, block in enumerate(P):
dvecs[blockNo] = [[-1] * numBlocks] * len(block)
for nodeNo, node in enumerate(block):
for otherBlockNo, otherBlock in enumerate(P):
dvecs[blockNo][nodeNo][otherBlockNo] = len(set(nx.neighbors(G, node)).intersection(set(otherBlock)))
I am, however, getting the same behavior. Did I miss something?