Home > Net >  Python: Returning 2D array seems to return the last row repeated multiple times
Python: Returning 2D array seems to return the last row repeated multiple times

Time:07-04

Here is the code:

import math

def create_dct_mat(size=8):
    
    rows, cols = (size, size)
    T = [[0.0]*cols]*rows

    for i in range(0,size):
        print("")
        for j in range(0,size):
            if i == 0:
                T[i][j] = 1/math.sqrt(size)
            else:
                T[i][j] = 2/math.sqrt(size)*math.cos(((2*j 1)*i*math.pi)/(2*size))
            print(T[i][j], end=", ")    
    
    return T

def main():

    size = 8
    rows, cols = (size, size)
    T = [[0.0]*cols]*rows    
    
    T = create_dct_mat(size)
    
    print("")
    
    for i in range(0,size):
        print("")
        for j in range(0,size):
            print(T[i][j], end=", ")


if __name__ == '__main__':
    main()

The output looks like this:

0.35355339059327373, 0.35355339059327373, 0.35355339059327373, 0.35355339059327373, 0.35355339059327373, 0.35355339059327373, 0.35355339059327373, 0.35355339059327373,
0.6935199226610737, 0.5879378012096793, 0.3928474791935511, 0.13794968964147153, -0.13794968964147145, -0.39284747919355084, -0.5879378012096794, -0.6935199226610737,
0.6532814824381882, 0.2705980500730985, -0.27059805007309845, -0.6532814824381882, -0.6532814824381883, -0.2705980500730989, 0.2705980500730986, 0.6532814824381881,
0.5879378012096793, -0.13794968964147145, -0.6935199226610737, -0.392847479193551, 0.3928474791935508, 0.6935199226610737, 0.13794968964147186, -0.5879378012096792,
0.5, -0.4999999999999999, -0.5000000000000001, 0.49999999999999983, 0.5000000000000001, -0.4999999999999994, -0.49999999999999967, 0.4999999999999993,
0.3928474791935511, -0.6935199226610737, 0.13794968964147153, 0.5879378012096795, -0.5879378012096792, -0.13794968964147133, 0.6935199226610738, -0.39284747919355056,
0.2705980500730985, -0.6532814824381883, 0.6532814824381881, -0.27059805007309856, -0.270598050073099, 0.6532814824381882, -0.653281482438188, 0.27059805007309834,
0.13794968964147153, -0.392847479193551, 0.5879378012096795, -0.6935199226610738, 0.6935199226610738, -0.5879378012096792, 0.39284747919355056, -0.13794968964147172,

0.13794968964147153, -0.392847479193551, 0.5879378012096795, -0.6935199226610738, 0.6935199226610738, -0.5879378012096792, 0.39284747919355056, -0.13794968964147172,
0.13794968964147153, -0.392847479193551, 0.5879378012096795, -0.6935199226610738, 0.6935199226610738, -0.5879378012096792, 0.39284747919355056, -0.13794968964147172,
0.13794968964147153, -0.392847479193551, 0.5879378012096795, -0.6935199226610738, 0.6935199226610738, -0.5879378012096792, 0.39284747919355056, -0.13794968964147172,
0.13794968964147153, -0.392847479193551, 0.5879378012096795, -0.6935199226610738, 0.6935199226610738, -0.5879378012096792, 0.39284747919355056, -0.13794968964147172,
0.13794968964147153, -0.392847479193551, 0.5879378012096795, -0.6935199226610738, 0.6935199226610738, -0.5879378012096792, 0.39284747919355056, -0.13794968964147172,
0.13794968964147153, -0.392847479193551, 0.5879378012096795, -0.6935199226610738, 0.6935199226610738, -0.5879378012096792, 0.39284747919355056, -0.13794968964147172,
0.13794968964147153, -0.392847479193551, 0.5879378012096795, -0.6935199226610738, 0.6935199226610738, -0.5879378012096792, 0.39284747919355056, -0.13794968964147172,
0.13794968964147153, -0.392847479193551, 0.5879378012096795, -0.6935199226610738, 0.6935199226610738, -0.5879378012096792, 0.39284747919355056, -0.13794968964147172,

The matrix is created correctly inside the create_dct_mat. However, when I loop through the value returned by this function, I end up seeing the last row repeated multiple times. Why is this so?

CodePudding user response:

This happened as a result of initializing 2D list with [[0.0]*cols]*rows. It creates a 1D list [0.0] * cols, and makes rows copies. You can see this problem for more discussion.

The solution is simple, you can change the way to initialize your 2D list:

T = [[0.0] * size for i in range(size)]
  • Related