Python:返回的2D数组似乎返回最后一行多次重复
这是代码:
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()
输出看起来像:
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,
矩阵是在Create_dct_mat内部正确创建的。但是,当我循环浏览该功能返回的值时,我最终会看到最后一行重复多次。为什么这样?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是由于用
[[0.0]*cols]*行
初始化2D列表的结果。它创建一个1D列表[0.0] * cols
,然后制作rows
副本。您可以看到此问题进行更多讨论。解决方案很简单,您可以更改初始化2D列表的方式:
This happened as a result of initializing 2D list with
[[0.0]*cols]*rows
. It creates a 1D list[0.0] * cols
, and makesrows
copies. You can see this problem for more discussion.The solution is simple, you can change the way to initialize your 2D list: