[0] * n 意味着创建一个包含 n 个零的列表。

huangapple go评论70阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年5月11日 18:27:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76226635.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定