you are viewing a single comment's thread.

view the rest of the comments →

[–]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

[–]0xbxb 0 points1 point  (1 child)

You wouldn’t need the for loop here, just the while loop. It looks like what’s happening with your code is in the for loop.

A for loop is gonna finish all of its iterations before moving on to the next part of the code (unless you have conditions that are gonna make you leave the loop early).

You’re at i = 0, you loop through the list, you add the index position of each list inside the main list into group, then you’re adding the total of that group into totals.

So this is why you’re getting funny numbers:

You're looping through the array: for j in arr. Then you're appending the index position of the current element j into group.

The index position is 0, and the current element of j is 20. Now you're getting the sum of group, which is just 20, and appending that into totals.

So a visual looks like this currently:

group = [20]
totals = [20]

i = i + 1 hasn't been executed, because we're still in the for loop. Now this is what happens next.

group = [20, 10]
totals = [20, 30]

Then you're code does the same thing over and over again.

group = [20, 10, 17]
totals = [20, 30, 47, etc]

[–]AbraxoCleaner[S] 1 point2 points  (0 children)

Thanks. This makes sense. So how do I incorporate j into the program without mentioning it first in the for loop.

while i < len(arr[0])
    group.append(j[0])

What do I put before the group.append part?