【发布时间】:2021-05-29 18:40:10
【问题描述】:
这应该是代码的工作方式:
n = int(input()) #number of rows, for example 3
m = int(input()) #number of columns, for example 5
然后用户将在一行输入一个值,然后为列输入一个值。
x = int(input()) #This should be the row number, for example row 1
y = float(input()) #This should be the value for each position inside of each x.
x 和 y 都需要遵循某些条件,所以我将它们分开。理论上它应该是这样的:
matrix = [ [0.4], [0.3, 0.2, 0.5], [0.7] ] #Row 1 has 1 float, Row 2 has 3 float and Row 3 only 1
一些浮点数将进入不同的行,例如第 1 行可以有来自输入的三个浮点数,而另一行(第 3 行)可能有来自输入的 5 个浮点数。
我尝试过使用以下循环:
for i in range(n): #I have tried multiple ways using len function, append function, etc.
for j in range(m):
但我似乎无法在矩阵上分配每个值,因为我必须确保输入遵循某些条件,因为程序应该读取与变量“m”一样多的不同浮点数。
我以这种方式详细说明代码的原因是因为我必须根据从浮点输入中获得的不同值计算平均值(在不同的函数中),使它们通过我已经拥有的公式以前做过。
【问题讨论】:
-
如果用户在开始时必须输入
m,列数,怎么可能有些行有更多列?m应该只是每行的最大列数吗? -
嘿@He3lixxx,感谢您的回复!在这种情况下,
m变为用户将要输入的y输入的数量。因此,如果m = 5将有 5 个不同的y输入,每个输入分配给由n数量确定的行号。我猜可能每列的其他位置为 0。
标签: python matrix input row multiple-columns