Contact: aviboots(AT)netvision.net.il
43,919 questions
57,323 answers
573 users
nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] num_columns = len(nested_list[0]) column_sum = [0] * num_columns for row in nested_list: for i in range(num_columns): column_sum[i] += row[i] print("The column sum:",column_sum) ''' run: The column sum: [12, 15, 18] '''
nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] column_sum = [sum(column) for column in zip(*nested_list)] print("The column sum:",column_sum) ''' run: The column sum: [12, 15, 18] '''
import numpy as np nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] arr = np.array(nested_list) column_sum = np.sum(arr, axis=0) print("The column sum:",column_sum) ''' run: The column sum: [12, 15, 18] '''