I am new to Python, and I'm struggling to program a function that fills a matrix.I was attempting to write a sensing function for a robot, yet I noticed something I wasn't quite getting. I fill a list of a list iterating it in a loop and checking a condition so as to know which value to assign. When I print the current element, the program seems to be giving me what I want, but when I print the entire list of list, it is filled with zeros. How is this possible? Thanks in advance
p=[[0.05,0.05,0.05,0.05, 0.05],[0.05,0.05,0.05,0.05, 0.05],[0.05,0.05,0.05,0.05, 0.05],[0.05,0.05,0.05,0.05, 0.05]]
measurement='G'
sensor_right=0.7
colors = [['R','G','G','R','R'],['R','R','G','R','R'],['R','R','G','G','R'],['R','R','R','R','R']]
sensetable= [ [0.0] * len(p[0]) ]*len(p)
for i in range(len(p)):
for j in range(len(p[0])):
if (colors[i][j]==measurement):
sensetable[i][j]=1
else:
sensetable[i][j]=0
print(sensetable)
UPDATE: Thank you so much for all of your help. This was all part of a function, which takes into account the probabilities stored in p. I attach the code below. This was the best way I thought it could be done by reading your replies. I do not see a way of using nested list comprehension in this case, where I do need p[i][j] in my calculations inside the loop. Please correct me if I am wrong or if you have further suggestions. Thank you so much!!
def sense(p,measurement,sensor_right,colors):
sensetable=[]
for i in range(len(p)):
aux=[]
for j in range(len(p[0])):
aux.append(p[i][j]*sensor_right if colors[i][j]==measurement else p[i][j]*(1-sensor_right))
sensetable.append(aux)
norm=sum(sum(sensetable,[]))
probTableFin = [[float(j)/norm for j in i] for i in sensetable]
return probTableFin