Code Input:
3 3
1 2 3
4 5 6
7 8 9
How to find the sum of each Row. This is the code for getting lists as input:
r, c = map (int, input().split())
lst = []
for _ in range(c):
arr=list (map (int, input().split()))
lst.append (arr[:r])?
Please check the below solution, you just have to add one line print(sum(arr)) as arr is a list, and sum(list) will give a summation of all the elements inside that list, or in case if in a list you just want to share a summation of all elements in a list then do: lst.append(sum(arr)).
r, c = map (int, input().split())
lst = []
for i in range(r):
arr = list (map (int, input().split()))
lst.append(arr)
for i in range(len(lst)):
print("Row {0} : {1}".format(i+1,sum(lst[i])))
If you're trying to get the sum of each seperate row then this should work
x = """
3 3
1 2 3
4 5 6
7 8 9
"""
strings = [i for i in x.split("\n") if i] # find the rows
for i in strings:
nums = sum([int(n) for n in i if n.isdigit()])
print(nums)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.