这是我的IT课程,我试图找到每一行的总和。它是一个神奇的方形程序。当我运行它时,它说int对象不可迭代。老师也想要函数的头像def rowSum(matrix,rowNum),但我不明白为什么需要rowNum。该函数应该能够计算任何大小矩阵的每一行的总和。我也不能使用numpy和enumarate,因为我们还没有在课堂上讨论它。
Matrix txt文件:
1 4
3 2
这是代码
def main():
filNam = "matrix1.txt"
matrix = (readMatrix(filNam))
print(eachNumPresent(matrix))
print(rowSum(matrix))
def readMatrix(filNam):
matrixFile = open(filNam, "r")
line = matrixFile.readline()
for line in matrixFile:
line = line.split()
return line
matrixFile.close()
def eachNumPresent(matrix):
if len(matrix) % 2 == 0:
return True
else:
print("Not enough numbers")
def rowSum(matrix, rowNum):
for line in matrix:
return(sum(int(line)))
main()
答案 0 :(得分:1)
这个怎么样,它假定当你想要第一行时,你输入'0'如果你想要第二行你输入'1'等等......很容易改变。
def main():
fn = "matrix1.txt"
matrix = readMatrix(fn)
n = int(input("Enter row number: "))
rowSum(matrix, n)
def readMatrix(fn):
matrix = []
with open(fn) as f: # a with block will auto close your file after the statements within it
for line in f:
line = line.strip() # strip off any trailing whitespace(including '\n')
matrix.append(line.split())
return matrix
def rowSum(matrix, rowNum):
result = sum(int(i) for i in matrix[rowNum])
print("The sum of row {} = {}".format(rowNum, result))
main()
示例输出:
The sum of row 1 = 5
编辑:
计算列的总和:
def main():
fn = "matrix1.txt"
matrix = readMatrix(fn)
n = int(input("Enter col number: "))
colSum(matrix, n)
def readMatrix(fn):
matrix = []
with open(fn) as f: # a with block will auto close your file after the statements within it
for line in f:
line = line.strip() # strip off any trailing whitespace(including '\n')
matrix.append(line.split())
return matrix
def colSum(matrix, colNum):
result = sum(int(row[colNum]) for row in matrix)
print("The sum of col {} = {}".format(colNum, result))
main()
答案 1 :(得分:1)
你没有迭代正确的序列,你使用“readline”而不是“readlines”,它只读取矩阵的一行,而不是整个文件。你想要做的是读取所有行,并将这些行转换为列表,因此你可以使用sum()。请注意,在拆分之后,您需要确保将生成的字符串转换为int。通过它,您可以将矩阵形成为列表列表(格式为[[1,2],[3,4]]),然后您可以将行号(从零开始,但可以调整)传递给rowSum方法(这就是为什么你的老师为你提供了这种格式的原因)
查看更正示例中的差异(我在不同的操作中列出了转换的步骤,但您实际上可以重构该代码以在更少的行中完成):
def main():
filNam = "matrix1.txt"
matrix = (readMatrix(filNam))
print(eachNumPresent(matrix))
print(rowSum(matrix, 0))
def readMatrix(filNam):
with open(filNam, 'r') as matrixFile:
matrix = []
lines = matrixFile.readlines()
for line in lines:
items = line.split()
numbers = [int(item) for item in items]
print numbers
matrix.append(numbers)
return matrix
def eachNumPresent(matrix):
if len(matrix) % 2 == 0:
return True
else:
print("Not enough numbers")
def rowSum(matrix, rowNum):
for line in matrix:
return(sum(line))
main()
如果您需要任何澄清,请告诉我......希望它有所帮助!
答案 2 :(得分:0)
1.你可以学习大熊猫
2.您的代码有错误,这是我的代码
def main():
filNam = "matrix1.txt"
rowNum, matrix = (readMatrix(filNam))
print(eachNumPresent(rowNum))
rowSum(matrix)
def readMatrix(filNam):
matrix_list = []
total_num = 0
with open(filNam, 'r') as matrixFile:
for line in matrixFile.readlines():
line = line.split()
line = [int(i) for i in line]
matrix_list.append(line)
total_num += len(line)
return total_num, matrix_list
def eachNumPresent(num):
if num % 2 == 0:
return True
else:
print("Not enough numbers")
def rowSum(matrix):
for row in matrix:
print sum(row)
main()
出:
True
5
5