Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 无法将数字输入MTAX。为什么指数超出范围?_Python 2.7 - Fatal编程技术网

Python 2.7 无法将数字输入MTAX。为什么指数超出范围?

Python 2.7 无法将数字输入MTAX。为什么指数超出范围?,python-2.7,Python 2.7,无法在矩阵中输入数字。为什么索引器出错:列表分配索引超出范围 i, j = 5, 7; matrix = [[x + y for x in xrange(i)] for y in xrange(j)] print (matrix) for w in xrange(i): print (w) for h in xrange(j): tmp = int(input('Enter element of matrix')) matrix[w][h] = t

无法在矩阵中输入数字。为什么索引器出错:列表分配索引超出范围

i, j = 5, 7;
matrix = [[x + y for x in xrange(i)] for y in xrange(j)]
print (matrix)
for w in xrange(i):
    print (w)
    for h in xrange(j):
        tmp = int(input('Enter element of matrix'))
        matrix[w][h] = tmp
sums = map( lambda row: sum(row), matrix)
print (matrix)
print (sums)
print ('max:', sums.index(max(sums)))
print ('min:', sums.index(min(sums)))
上一行使列数=i,行数=j,因为它将创建一个带有i变量的j列表,其中每个列表充当一行

for w in xrange(i):
print (w)
for h in xrange(j):
    tmp = int(input('Enter element of matrix'))
    matrix[w][h] = tmp
在这个循环中,使用w作为行,范围从0到i-1,而应该是0到j-1 同样,h的范围应为0到i-1,而不是0到j-1 所以你的循环应该是这样的-

for w in xrange(j):  #note this changed from i to j
print (w)
for h in xrange(i):  #and this from j to i
    tmp = int(input('Enter element of matrix'))
    matrix[w][h] = tmp

我是内在的标记。只需在init或循环中交换i&j即可。这比我想的更简单:Dalso:lambda row:sumrow=>sum:
for w in xrange(j):  #note this changed from i to j
print (w)
for h in xrange(i):  #and this from j to i
    tmp = int(input('Enter element of matrix'))
    matrix[w][h] = tmp