Sum different crescent values to each int in a list (python)

Question:

I have this little code that appends the sum of first n numbers, after that the sum of first n-1 numbers, the sum of first n-2 numbers and so on.

The way I made it may not be the simplest but it just works for me

    a = [1,2,3,4,5,6,7,8,9,10]
    i = 1
    b = []
    while i < 11:
        b.append(sum(a))
        a.pop()
        if len(a) == 1:
            print(b)
            break

This is of course the output:

    b = [55,45,36,28,21,15,10,6,3]

Now, I want to create some new lists based on the values of b, in this way:

    b[0] + 10
    b[1] + 9
    b[2] + 8
    b[3] + 7
    b[4] + 6
    b[5] + 5 
    b[6] + 4
    b[7] + 3
    b[8] + 2

But I have no idea of making this. Please give me some suggestions. Bye!

Andrea

EDIT

Thanks to all for the support. The simplest way of making what I need I think was

c = [x+10-i for i,x in enumerate(b)]
print(c)

but there is a way to iterate this process starting from the c variable and so on?

EDIT 2

I think I resolved, of course using your help. I wrote:

     a = list(range(1,11)
     i = 0
     b = []
     while i < 11
         b.append((sum(a))
         a.pop()
         if len(a) == 1
         print(b)
         break

     numbers = [None] * 10

     numbers[i] = [x+10-i for i,x in enumerate(b)]
     print(numbers[0])
     for i in range(1,9):
         numbers[i] = [x+10-i for i,x in enumerate(numbers[i-1])
         print(numbers[i])

Of course it is something like spaghetti but it works. Every suggestion about more clear solutions are welcomed!

Bye, Andrea

Asked By: Andrea

||

Answers:

You could enumerate over the results, adding 10 and subtracting the index you’re on:

>>> c = [x+10-i for i,x in enumerate(b)]
>>> c
[65, 54, 44, 35, 27, 20, 14, 9, 5]

this could also easly achievable between the zip function:

>>> c = [x+y for x,y in zip(b,range(10,2,-1))]

this list comprehension is equivalent as the following for loop:

>>> c = []
>>> for x,y in zip(b,range(10,2,-1)):
...    c.append(x + y)
>>> c
[65, 54, 44, 35, 27, 20, 14, 9, 5]
Answered By: XxJames07-

I would use list(reversed(b)) to get b reversed then iterate though the reversed list and add i to get a

Answered By: Taonga07

Your solution works but does not make use of Python’s facilities for iteration. Also, you don’t need to keep track of the sum, you can rely on the fact that the sum of elements from i to n is actually equal to (n * (n+1) / 2).

For example,

a = list(range(1, 11))

def gauss_trick(l):
    out = []
    for i in l:
        out.append((i * (i + 1)) // 2)
    return out

will return [1, 3, 6, 10, 15, 21, 28, 36, 45, 55], you can see the reverse with:

b = gauss_trick(a)
print(b[::-1]) # list reversal
print(b[1:][::-1]) # list reversal after skipping the first element

Then you can add a value based on the index during iteration, it’s easily accessed with enumerate, the commented out version will store the output in another list rather than modifying the list in place, if you so desire.

def idx_sum(l):
#   out = []
    for idx, el in enumerate(l): # gets you index, val for each elem
        # out.append(el + idx + 1)
        l[idx] = el + idx + 1

Which outputs:

[2, 5, 9, 14, 20, 27, 35, 44, 54, 65]
# You can always ignore the first value with out[1:]
# You can always reverse with out[::-1]

Using the [::-1] also has the advantage of not modifying the list, which means you can use both the reversed and original version of the list only based on how you slice it.

Answered By: Nathan Furnal
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.