We have a list say list1 = [2, 1, 4]
I made a 2d list called subl of the subsets of list1 with following code.
def sub_lists(list1):
sublist = [[]]
for i in range(0, len(list1) - 1):
for j in range(i + 1, len(list1) + 1):
sub = list1[i:j]
sublist.append(sub)
return sublist
The output I got is
subl = sub_lists(list1) #[[], [2], [2, 1], [2, 1, 4], [1], [1, 4]]
Now I want to make an array of sums of the list elements present in in subl I wrote following code but its not working correctly.
sum = [];
for i in range(0, len(subl) - 1):
l = len(subl[i])
t = 0
for j in range(0, l - 1):
t = t + subl[i][j]
sum.append(t)
print(sum)
The output I'm getting is
[0, 0, 2, 3, 0]
While the desired output should be
[0,2,3,7,1,5]
I guess there's something wrong in the second for loop. Please help!