如何使用python中的for循环添加2D数组中的值
if __name__ == '__main__':
scores = [[8, 1], [5, 3], [3, 5], [8, 1], [7, 1], [1, 0], [9, 0], [5, 0], [6, 0], [8, 1, 0]]
#scores_int = 0;
#scores_int = len(scores);
sum = 0;
for row in range(len(scores)):
sum = scores[0]
print(sum)
print(scores[0:1])
print(scores[1][0])
#print("Sum of all all the socres:", sum);
因此,我试图找出一种循环浏览数组的每个实例,并将值添加到总和变量中的方法,但是我不明白如何循环循环2D数组。得分总和应等于72 我只需要语法帮助for循环
if __name__ == '__main__':
scores = [[8, 1], [5, 3], [3, 5], [8, 1], [7, 1], [1, 0], [9, 0], [5, 0], [6, 0], [8, 1, 0]]
#scores_int = 0;
#scores_int = len(scores);
sum = 0;
for row in range(len(scores)):
sum = scores[0]
print(sum)
print(scores[0:1])
print(scores[1][0])
#print("Sum of all all the socres:", sum);
So im trying to figure out a way to loop through every instance of the array and add the values in them to a sum variable, but i dont understand how to loop through a 2d array. The score sum should equal 72
i just need syntax help with the for loop
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的建议,如果您只是为了尝试您不知道的事情。
下次您可以打印所有数字列表,然后尝试将其概括,avg或计算其中的数字
My advice if you're begginer is just to experiment with things you don't know.
Next time you could print all the list of numbers and then try to sum it , avg it, or count the numbers inside it