我对Python编程很陌生。我的任务是:
对于这个实验室,您将使用Python中的二维列表。做以下工作:
示例程序运行如下:
输入第0: 2.5 3 4 1.5行的矩阵行3乘4矩阵行1: 1.5 4 2 7.5输入第2行3乘4矩阵行: 3.5 1 1 2.5矩阵行
矩阵为2.5 3.0 4.0 1.5 1.5 4.0 2.0 7.5 3.5 1.0 1.0 2.5
第0栏的元素之和为7.5,第1栏的元素和为8.0,第2栏的元素和为7.0,第3栏的元素和为11.5
下面是我到目前为止掌握的代码:
def sumColumn(matrix, columnIndex):
total = (sum(matrix[:,columnIndex]) for i in range(4))
column0 = (sum(matrix[:,columnIndex]) for i in range(4))
print("The total is: ", total)
return total
def main ():
for r in range(3):
user_input = [input("Enter a 3-by-4 matrix row for row " + str(r) + ":", )]
user_input = int()
rows = 3
columns = 4
matrix = []
for row in range(rows):
matrix.append([numbers] * columns)
print (matrix)
main()
it prints out:
[[0, 0, 0, 0]]
[[0, 0, 0, 0], [0, 0, 0, 0]]
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]我做错了什么?
发布于 2016-01-17 17:06:25
你应该把它当作参考,否则你什么都学不到
PROMPT = "Enter a 3-by-4 matrix row for row %s:"
def sumColumn(matrix, columnIndex):
return sum([row[columnIndex] for row in matrix])
def displayMatrix(matrix):
#print an empty line so that the programs output matches the sample output
print
print "The matrix is"
for row in matrix:
print " ".join([str(col) for col in row])
#another empty line
print
for columnIndex in range(4):
colSum = sumColumn(matrix, columnIndex)
print "Sum of elements for column %s is %s" % (columnIndex, colSum)
def main ():
matrix = [map(float, raw_input(PROMPT % row).split()) for row in range(3)]
displayMatrix(matrix)
if __name__ == "__main__":
main()https://stackoverflow.com/questions/34840514
复制相似问题