Click here to Skip to main content
15,884,991 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
rows = [["SOBs", 60, 80, 70, 75],
["Test1", 60, 50, 60, 65],
["Test2", 40, 30, 40, 45],
["Test3", 45, 90, 80, 85],
["CW", 40, 80, 70, 75]]


I need to add the first element from each list. So 60+60+40+45+40. But the string needs to stay.

What I have tried:

sum(sum(rows, [2]))


print(sum(rows[0][1] + [1][1]))


I tried overload as well, but that requires two separate lists. Same seems to be true for numpy.
Posted
Updated 21-Nov-21 21:23pm

1 solution

got it from stackoverflow

element = 1
rows = [["SOBs", 60, 80, 70, 75], ["Test1", 60, 50, 60, 65], ["Test2", 40, 30, 40, 45], ["Test3", 45, 90, 80, 85], ["CW", 40, 80, 70, 75]]
sum_1= sum([rows[i][element] for i in range(len(rows))])
element = 2
sum_2= sum([rows[i][element] for i in range(len(rows))])
print((sum_1*100)/(sum_1+sum_2))
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900