I have a simple two-dimensional List in python. I create it by reading in a text file, line by line, and using the .split() method to divide it up, convert to float, and all seems fine. I immediately print the list, index by index and it looks good. I do it a second time, and suddenly it only contains what was in the last index of the original list. Except for the split() and float assignment I literally copied the code from the top loop to make the second loop:
dataLinesStr = ["1 2 3 4 5", "6 7 8 9 10", "11 12 13 14 15", "16 17 18 19 20", "21 22 23 24 25"]
numLines = len(dataLinesStr)
numPix = 5
lineData = [[0.0] * numPix] * numLines # Refer as: lineData[numLines][numPix]
r = 0
while r < numLines:
tmpStr = dataLinesStr[r].split() # a list of strings.
p = 0
while p < numPix:
lineData[r][p] = float(tmpStr[p])
if r < 3: print("[" + str(r) + "][" + str(p) + "]lineData = ", lineData[r][p])
p += 1
r += 1
print("2nd time....")
r = 0
while r < numLines:
p = 0
while p < numPix:
if r < 3: print("[" + str(r) + "][" + str(p) + "]lineData = ", lineData[r][p])
p += 1
r += 1
Output is:
[0][0]lineData = 1.0
[0][1]lineData = 2.0
[0][2]lineData = 3.0
[0][3]lineData = 4.0
[0][4]lineData = 5.0
[1][0]lineData = 6.0
[1][1]lineData = 7.0
[1][2]lineData = 8.0
[1][3]lineData = 9.0
[1][4]lineData = 10.0
[2][0]lineData = 11.0
[2][1]lineData = 12.0
[2][2]lineData = 13.0
[2][3]lineData = 14.0
[2][4]lineData = 15.0
2nd time....
[0][0]lineData = 21.0
[0][1]lineData = 22.0
[0][2]lineData = 23.0
[0][3]lineData = 24.0
[0][4]lineData = 25.0
[1][0]lineData = 21.0
[1][1]lineData = 22.0
[1][2]lineData = 23.0
[1][3]lineData = 24.0
[1][4]lineData = 25.0
[2][0]lineData = 21.0
[2][1]lineData = 22.0
[2][2]lineData = 23.0
[2][3]lineData = 24.0
[2][4]lineData = 25.0
There are no variable manipulations between the two loops, yet only the last element in the original list is present in the second loop. I've been going nutz over this. I can't see a mutable vs. nonmutable reason this is occurring. Anyone see what's wrong here to produce this?