Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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 在二维列表中添加列表_Python_List - Fatal编程技术网

Python 在二维列表中添加列表

Python 在二维列表中添加列表,python,list,Python,List,为什么会在下面抛出错误 marks = [[12,14,8,7,17],[6,8,5,3,13],[16,15,9,10,18]] total = 0 for s in range(3): for m in range(5): total[s] = total[s] + marks [s][m] print("total for student",s,"is",total[s]) 我们可以简化您的代码并使其工作 代码: TypeError: 'int'

为什么会在下面抛出错误

marks = [[12,14,8,7,17],[6,8,5,3,13],[16,15,9,10,18]]

total = 0
for s in range(3):
    for m in range(5):
        total[s] = total[s] + marks [s][m]
        print("total for student",s,"is",total[s])

我们可以简化您的代码并使其工作

代码:

TypeError: 'int' object is not subscriptable
marks = [[12,14,8,7,17],[6,8,5,3,13],[16,15,9,10,18]]    
total = 0
for s in range(len(marks)):
    print("total for student",s,"is",sum(marks[s]))
total for student 0 is 58
total for student 1 is 35
total for student 2 is 68
输出:

TypeError: 'int' object is not subscriptable
marks = [[12,14,8,7,17],[6,8,5,3,13],[16,15,9,10,18]]    
total = 0
for s in range(len(marks)):
    print("total for student",s,"is",sum(marks[s]))
total for student 0 is 58
total for student 1 is 35
total for student 2 is 68
对代码进行更改

代码1:

TypeError: 'int' object is not subscriptable
marks = [[12,14,8,7,17],[6,8,5,3,13],[16,15,9,10,18]]    
total = 0
for s in range(len(marks)):
    print("total for student",s,"is",sum(marks[s]))
total for student 0 is 58
total for student 1 is 35
total for student 2 is 68
注意事项:

TypeError: 'int' object is not subscriptable
marks = [[12,14,8,7,17],[6,8,5,3,13],[16,15,9,10,18]]    
total = 0
for s in range(len(marks)):
    print("total for student",s,"is",sum(marks[s]))
total for student 0 is 58
total for student 1 is 35
total for student 2 is 68
  • 由于您试图计算列表的总和,因此可以使用
    sum
    方法

如果您要浏览列表,您实际上不需要知道列表的长度。如果您这样做是为了编制索引,请改用

而且,正如第六感所说的,用它来计算每个学生的名单

提示:使用使您的代码更加优雅!:-)


可能是因为
total
是一个int,你不能在int上调用
[s]
…我是这样的,我该怎么办?用
total+=marks[s][m]
来代替。这仍然会引发一个错误,只需将total更改为一个列表,
total=[]
我希望学生0的总数为58学生1的总数为35学生2的总数为68@AlanRobinson我在这里给出的输出不是吗?月份=[“一月”、“二月”、“三月”、“四月”]温度=[]a=0,每个月:温度=int(输入(“请输入”,月份[a]))a=a+1@AlanRobinson我不能带你去那里