【发布时间】:2021-03-01 03:48:38
【问题描述】:
预期输出:
indenitiy_matrix(3)
[[1, 0, 0], [0, 1, 0], [0, 0, 1]]
有错误的实际输出:
indenitiy_matrix(3)
[[1, 1, 1], [1, 1, 1], [1, 1, 1]]
def identity_matrix(n):
list_template = [[]]
list_n = list_template*n
for sub_l in list_n:
sub_l.append(0)
for val in range(n):
# I have the feeling that the problem lies somewhere around here.
list_n[val][val]=1
return(list_n)
【问题讨论】:
-
您的问题是由本题解释的问题引起的:stackoverflow.com/questions/240178/…
标签: python-3.x list for-loop matrix computer-science