The command sum does not work in a matrix, how do I add all the numbers correctly? | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
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

15th Jul 2022, 1:49 PM
Alba Robledo
Alba Robledo - avatar
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
15th Jul 2022, 2:52 PM
Lothar
Lothar - avatar
+ 4
print(sum(data[0]) + sum(data[1]))
15th Jul 2022, 1:56 PM
JaScript
JaScript - avatar
+ 4
sum([sum(num_list) for num_list in data])
15th Jul 2022, 1:56 PM
Slick
Slick - avatar
+ 3
Have a look at the numpy module: It has an array data type that allows calculating sums and much more
15th Jul 2022, 2:21 PM
Lisa
Lisa - avatar
+ 2
total = sum(map(sum, data))
15th Jul 2022, 3:29 PM
Tibor Santa
Tibor Santa - avatar
0
Solved, thanks all
15th Jul 2022, 3:30 PM
Alba Robledo
Alba Robledo - avatar