英文:
What does [0] * n mean?
问题
The code maxsum = [0] * len(N)
initializes a list called maxsum
with a length equal to the number of rows in your matrix N
. Each element in this list is initialized to 0.
The +1
in the output is used to provide a human-readable row number since Python uses 0-based indexing for lists. So, when the code prints the row number with maxsum.index(max(maxsum)) + 1
, it adds 1 to the index to make it match conventional 1-based row numbering for humans.
英文:
I created a matrix, brought it out and then the code according to my question:
maxsum = [0] * len(N)
for i in range(a):
maxsum[i] = sum(N[i])
print(f'максимальная cтрока {maxsum.index(max(maxsum))+1} сумма {max(maxsum)}',
f'минимальная cтрока {maxsum.index(min(maxsum))+1} сумма {min(maxsum)}',
sep="\n", end=" ")
What does it mean to multiply [0]
by the length of my matrix and why add 1 in the output?
The code works, I want you to comment, explain this: maxsum = [0] * len(N)
.
答案1
得分: 1
你正在创建一个只包含零的列表,其中元素数量等于矩阵的总行数。
例如,[0]*5
将会得到 [0,0,0,0,0]
。然后通过循环,你将第i行的和赋值给列表的第i个索引,并打印出最大值的索引。
我会说这有点像为列表中所有行的总和分配内存。
英文:
You are creating a list of only zeroes where the number of elements =
total rows of matrix.
For example, [0]*5
would give [0,0,0,0,0]
. Then through loops, you are assigning the sum of ith row to ith index of the list and printing the index of maximum value.
I would say that it's kind of like allocating memory for the sum of all rows in the list
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论