Solved, thanks to everyone for their help! My array generation code was referencing the same array, so edits would apply to whole columns instead of specific points.
I'm making a few functions I can use in future programs in Python to do with arrays, such as defining them and displaying them. However, I came across a strange bug or glitch while trying to set values in order in an array from left to right. For some reason, when the program sets values on the last y value, (or actually on any value,) it sets that value for the whole column instead for just one, even though I only have 2 loops. Here's my code:
def gen(xLen, yLen, fill = 0):
mainArr = list()
secArr = list()
for i in range(xLen):
secArr.append(fill)
for i in range(yLen):
mainArr.append(secArr)
return mainArr
def sums(xLen, yLen):
newArr = gen(xLen, yLen)
a = 0
for y in range(yLen):
for x in range(xLen):
newArr[y][x] = a
print(str(x) + ", " + str(y) + " = " + str(a)) #For debugging, what the array SHOULD contain
a += 1
return newArr
(Just run this with print(sums(5, 5))
)
Instead of returning with [[0, 1, 2, 3, 4], ... [20, 21, 22, 23, 24]]
, it returns with a list full of [20, 21, 22, 23, 24]
and I really don't know why.
I don't want to append a new list to another list with values already in them, for example arr.append([0, 1, 2, 3, 4])
, because the array is already generated.
Why doesn't this work??? It's been bugging me for weeks!