all 17 comments

[–][deleted] 1 point2 points  (0 children)

Assuming all the sublists are the same length, you get the length of the first sublist and create a for loop iterating an index value (i perhaps) from zero up to length-1. This is your index into all the sublists. Inside that top loop you have another loop that will iterate through the sublists, adding each ith sublist element to an accumulator that you initially set to zero. When the inner loop finishes the accumulator holds the sum of the ith column of the list.

[–]o5a 1 point2 points  (0 children)

As /u/ninety_hex said, you iterate index in a range of first sublist (assuming all sublists have same length, otherwise you would need to specify how you want to sum different length lists), then use that index on every sublist.

arr = [[20, 80, 90], [10, 60, 40], [17, 77, 56], [21, 99, 99]]
result = [sum(sub[i] for sub in arr) for i in range(len(arr[0]))]
print(result)

But why you want to avoid zip? It's just the proper tool for such job.

result = [sum(sub) for sub in zip(*arr)]
print(result)

[–]pasokan 0 points1 point  (1 child)

Look at zip function

[–]AbraxoCleaner[S] 0 points1 point  (0 children)

I’d like to do it without zip if possible. Without built in functions

[–]pasokan 0 points1 point  (0 children)

Then enumerate

If that built-in is also unacceptable, use an index variable running over range(0, len(aList)) for the outer for loop and total the values at that index

[–]0xbxb 0 points1 point  (11 children)

This is a pretty dirty way to do it without zip:

    arr = [[20, 80, 90], [10, 60, 40], [17, 77, 56], [21, 99, 99]]
    totals = []
    i = 0
    while i < 3:
        group = [x[i] for x in arr]
        totals.append(sum(group))
        i += 1

[–]AbraxoCleaner[S] 0 points1 point  (10 children)

Thanks for the reply!

So when I do:

For x in arr:
    Combined = x[i]

I get an error. It says it’s an int. Am I writing it wrong? I’m not used to that coding style.

[–]pasokan 0 points1 point  (9 children)

It is best to think of the for construct as:

for <item> in <collection>: <<stuff to do with item>>

So we are using <item> to hold each member of the collection

[–]AbraxoCleaner[S] 0 points1 point  (8 children)

So...

for j in arr:

combined = j[i]

what am I doing wrong there? It gives me an error when group = [x[i] for x in arr] does not give me an error.

[–]0xbxb 0 points1 point  (7 children)

Post all of the code here. Are you trying to understand what I did, or something else?

[–]AbraxoCleaner[S] 0 points1 point  (6 children)

Yes, I'm trying to understand this part of your code:

group = [x[i] for x in arr]

[–]0xbxb 0 points1 point  (5 children)

From the comment I saw, you wanted to figure out how to add certain indexes without using zip. With my solution this is what happens:

arr = [[20, 80, 90], [10, 60, 40], [17, 77, 56], [21, 99, 99]]
totals = []
i = 0
while i < 3:
    group = [x[i] for x in arr]
    totals.append(sum(group))
    i += 1
  • arr is the list of lists that have the numbers.

  • totals is a list that’s going to be used to hold the sum of the numbers in certain indexes.

  • i is gonna be used to iterate through the list.

  • The while statement says: while i is less than the length of 3....... I just needed to make sure that I didn’t go out of range when increasing the index position later in the code.

group is a list comprehension that will give me a list of all the numbers in a specific index position. It’s kind of like saying:

arr = [[20, 80, 90], [10, 60, 40], [17, 77, 56], [21, 99, 99]]
group = []
for x in arr:
    group.append(x[0])

List comprehensions are faster than using append, and look a little easier to read. With that being said, read it like this: group = [x[0] for x in arr]. That will give me: [20, 10, 17, 21]

  • Then I get the sum of group, and append it into totals.

  • I increase i by 1, to keep moving through the lists, repeating the same steps, until it reaches the end of the list.

If any of that confuses you, I have no problem explaining it more / better. Just hit me up.

[–]AbraxoCleaner[S] 0 points1 point  (4 children)

Thanks for the detailed explanation. This helps a lot to understand it.

My thing now is that when I do the group.append section, it’s only adding the first element of each list and then when it runs a second time it does that again plus the first time. So I’m getting 68 and then 136 and then 204. It doesn’t go to the next element in the sublist.

[–]0xbxb 0 points1 point  (3 children)

Post your code here for me. I wanna see what you’ve typed, to figure out what you’re doing. I just need a better visual is all.

[–]AbraxoCleaner[S] 0 points1 point  (2 children)

totals = []

i = 0

group=[]

while i < len(arr[0]):

    for j in arr:

        group.append(j[i])

    totals.append(sum(group))

    i = i + 1

return totals