Jun-17-2020, 02:57 AM
Hi, I'm facing this really weird issue.I've been appending different lists to my list but while i print the result it shows that the last value appended is only added as many times as I appended different lists. My code and output will clear out exactly what I meant.
Here is my code:
I think it has something to do with memory addresses, but don't have a good idea about it.
Any suggestions will be very helpful.
Thank you!
Here is my code:
def generate_MagicSquare(n): mat=[[0 for i in range(n)]for i in range(n)] i,j=n//2,n-1 mat[i][j]=1 number_inserted=1 a=n**2 while number_inserted<a: i,j=i-1,j+1 if i==-1 and j==n: i,j=0,n-2 elif i==-1 or j==n: if i==-1: i=n-1 if j==n: j=0 else: pass if mat[i][j]!=0: j-=2 i+=1 number_inserted+=1 mat[i][j]=number_inserted return mat def rotate_Clockwise_90(B): n1=len(B[0]) for i in range(n1//2): for j in range(i,n1-i-1): temp=B[i][j] B[i][j]=B[n1-1-j][i] B[n1-1-j][i]=B[n1-1-i][n1-1-j] B[n1-1-i][n1-1-j]=B[j][n1-1-i] B[j][n1-1-i]=temp return B x=[] mat=generate_MagicSquare(3) print(mat) x.append(mat) print(x) a=rotate_Clockwise_90(mat) print(a) x.append(a) print(x) b=rotate_Clockwise_90(a) #Rotate by 180 print(b) x.append(b) print(x)Output:
Output:[[2, 7, 6], [9, 5, 1], [4, 3, 8]]
[[[2, 7, 6], [9, 5, 1], [4, 3, 8]]]
[[4, 9, 2], [3, 5, 7], [8, 1, 6]]
[[[4, 9, 2], [3, 5, 7], [8, 1, 6]], [[4, 9, 2], [3, 5, 7], [8, 1, 6]]]
[[8, 3, 4], [1, 5, 9], [6, 7, 2]]
[[[8, 3, 4], [1, 5, 9], [6, 7, 2]], [[8, 3, 4], [1, 5, 9], [6, 7, 2]], [[8, 3, 4], [1, 5, 9], [6, 7, 2]]]
Process finished with exit code 0
I think it has something to do with memory addresses, but don't have a good idea about it.
Any suggestions will be very helpful.
Thank you!