无法将 map() 的结果分配给二维列表
Unable to assign result of map() to 2-D list
下面的代码是在数组中取出一组学生分数行,需要找到分数最高的行。
下面是不完整的代码,因为仍然需要搜索最大总和行;但由于错误而停留在不完整的代码上。
它在 py3.codeskulptor.org/, while in Pythontutor 中给出了以下错误,程序在相同的行号处终止。
Line #17: IndexError: list assignment index out of range
# Input number of tests
t = int(input("Input number of tests"))
# Input size of rows (of students)
n = int(input("Input size of rows"))#.strip().split())
print (t,n)
arr = [[[] for i in range(t)] for i in range(n)]
total = [[] for i in range (n)]
for i in range(0,t):
# Input score of each student in row
sum =0
for j in range(n):
arr[i][j] = map(int, input("Input score").strip().split())#[:n]
#the above line causes compilation error
# Find sum of all scores row-wise
for m in arr[i][j]:
sum += m
total[i] = sum1
# Find the max. of total
for j in range(t):
y = max(total[j])
还请提出一个更清晰的方法来解决上述问题。
P.S. 我发现上面的代码几乎是错误的,但把我踢到了正确的方向。
将下面的修改代码与调试过程中出现的问题有关:
max() giving precedence to [] over an integer value
max() 的这种行为是在将第 13 行错误地表述为:
时检测到的
total = [[[] for i in range (l)] for i in range (n)]
正确的工作代码如下,输出 :
# Input number of tests
t = int(input("Input number of tests"))
# Input number of rows (of students)
n = int(input("Input number of rows"))#.strip().split())
# Input limit on number of students in a row (of students)
l = int(input("Input max. number of studentsin any row"))#.strip().split())
print ('t :',t,'n :',n, 'l :', l)
arr = [[[[] for i in range(l)] for i in range(n)] for i in range(t)]
total = [[[] for i in range (n)] for i in range (t)]
# run input of tests
for i in range(t):
# Input score of each student in the i-th row, out of n
sum =0
for j in range(n):
#for k in range(l):
print("jkl:","i:",i,"j:",j)
arr[i][j] = map(int, input("Input score").strip().split())[:l]
for i in range(t):
for j in range(n):
# Find sum of all scores row-wise
sum = 0
for m in arr[i][j]:
#sum[i][j][] += m
print ('m :', m)
sum += m
#total[i][j] = sum[i][j][]
total[i][j] = sum
for i in range(t):
print("Test no. ", i)
for j in range(n):
#for m in arr[i][j]:
print (arr[i][j])
#print (m)
print("=========")
for i in range(t):
for j in range(n):
print(i,"-",j, total[i][j])
print("::::::")
print("=========")
# Find the max. of total
for i in range(t):
print([m for m in total[i]])
y = max([m for m in total[i]])
print ('i:',i,',', 'max total:',y)
请求 max() 显示的优先级的原因,如果可能的话 link 参考 CPython 中的底层实现的答案。
arr = [[[] for i in range(t)] for i in range(n)]
考虑到 arr
的上述构造,您在嵌套循环中交换了 t
和 n
:
for i in range(t):
# …
for j in range(n):
arr[i][j] = …
进入arr
的第一个索引(对应于outer循环)必须是0到n之间的数字– 1.
进入arr
的第二个索引(对应于内部循环)必须是0到t之间的数字– 1.
你把 t 和 n 搞错了
我应该在 range(n)
j 应该在 range(t)
下面的代码是在数组中取出一组学生分数行,需要找到分数最高的行。
下面是不完整的代码,因为仍然需要搜索最大总和行;但由于错误而停留在不完整的代码上。
它在 py3.codeskulptor.org/, while in Pythontutor 中给出了以下错误,程序在相同的行号处终止。
Line #17: IndexError: list assignment index out of range
# Input number of tests
t = int(input("Input number of tests"))
# Input size of rows (of students)
n = int(input("Input size of rows"))#.strip().split())
print (t,n)
arr = [[[] for i in range(t)] for i in range(n)]
total = [[] for i in range (n)]
for i in range(0,t):
# Input score of each student in row
sum =0
for j in range(n):
arr[i][j] = map(int, input("Input score").strip().split())#[:n]
#the above line causes compilation error
# Find sum of all scores row-wise
for m in arr[i][j]:
sum += m
total[i] = sum1
# Find the max. of total
for j in range(t):
y = max(total[j])
还请提出一个更清晰的方法来解决上述问题。
P.S. 我发现上面的代码几乎是错误的,但把我踢到了正确的方向。
将下面的修改代码与调试过程中出现的问题有关:
max() giving precedence to [] over an integer value
max() 的这种行为是在将第 13 行错误地表述为:
total = [[[] for i in range (l)] for i in range (n)]
正确的工作代码如下,输出
# Input number of tests
t = int(input("Input number of tests"))
# Input number of rows (of students)
n = int(input("Input number of rows"))#.strip().split())
# Input limit on number of students in a row (of students)
l = int(input("Input max. number of studentsin any row"))#.strip().split())
print ('t :',t,'n :',n, 'l :', l)
arr = [[[[] for i in range(l)] for i in range(n)] for i in range(t)]
total = [[[] for i in range (n)] for i in range (t)]
# run input of tests
for i in range(t):
# Input score of each student in the i-th row, out of n
sum =0
for j in range(n):
#for k in range(l):
print("jkl:","i:",i,"j:",j)
arr[i][j] = map(int, input("Input score").strip().split())[:l]
for i in range(t):
for j in range(n):
# Find sum of all scores row-wise
sum = 0
for m in arr[i][j]:
#sum[i][j][] += m
print ('m :', m)
sum += m
#total[i][j] = sum[i][j][]
total[i][j] = sum
for i in range(t):
print("Test no. ", i)
for j in range(n):
#for m in arr[i][j]:
print (arr[i][j])
#print (m)
print("=========")
for i in range(t):
for j in range(n):
print(i,"-",j, total[i][j])
print("::::::")
print("=========")
# Find the max. of total
for i in range(t):
print([m for m in total[i]])
y = max([m for m in total[i]])
print ('i:',i,',', 'max total:',y)
请求 max() 显示的优先级的原因,如果可能的话 link 参考 CPython 中的底层实现的答案。
arr = [[[] for i in range(t)] for i in range(n)]
考虑到 arr
的上述构造,您在嵌套循环中交换了 t
和 n
:
for i in range(t): # … for j in range(n): arr[i][j] = …
进入arr
的第一个索引(对应于outer循环)必须是0到n之间的数字– 1.
进入arr
的第二个索引(对应于内部循环)必须是0到t之间的数字– 1.
你把 t 和 n 搞错了
我应该在 range(n)
j 应该在 range(t)