0
The command sum does not work in a matrix, how do I add all the numbers correctly?
data = [ [23, 11, 5, 14], [8, 32, 20, 5] ] I want to add all the numbers in data
6 ответов
+ 5
Alba Robledo ,
can you please share us your attempt?
here some hints how it can be done:
> we can use itertools chain, then do summing
> if there are more nested levels than 2, it makes sense to create a general flatten() function , then do summing
+ 4
print(sum(data[0]) + sum(data[1]))
+ 4
sum([sum(num_list) for num_list in data])
+ 3
Have a look at the numpy module: It has an array data type that allows calculating sums and much more
+ 2
total = sum(map(sum, data))
0
Solved, thanks all