Python Forum
Sum of elements on the list recursive
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sum of elements on the list recursive
#3
I deduct that if your 'iteration version works flawlessly' then objective is:

[1,0,2,0,0,3,1,3,2,1] --> [13, 12, 12, 10, 10, 10, 7, 6, 3, 1]

I think that your solution is too complicated.

Lists support sum(), so:

>>> sum([1, 2])
3
Lists also support slicing:

>>> a = [1, 2, 3, 4, 5]
>>> a[1:]
[2, 3, 4, 5]
>>> sum(a[1:])
14
Combining those two things into list comprehension and taking into account noisefloor comment about Python antipattern you can write simply:

>>> a = [1,0,2,0,0,3,1,3,2,1] 
>>> [sum(a[i:]) for i, v in enumerate(a)]
[13, 12, 12, 10, 10, 10, 7, 6, 3, 1]
Iterative solution should give you pretty good idea how to proceed with recursive solution.

EDIT: how Python core developers do it

There is built-in module itertools which have function accumulate (Make an iterator that returns accumulated sums).

There is rough equivalent provided:

def accumulate(iterable, func=operator.add):
    'Return running totals'
    # accumulate([1,2,3,4,5]) --> 1 3 6 10 15
    # accumulate([1,2,3,4,5], operator.mul) --> 1 2 6 24 120
    it = iter(iterable)
    try:
        total = next(it)
    except StopIteration:
        return
    yield total
    for element in it:
        total = func(total, element)
        yield total
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Messages In This Thread
Sum of elements on the list recursive - by sev - Jun-20-2019, 08:15 AM
RE: Sum of elements on the list recursive - by perfringo - Jun-20-2019, 09:40 AM
RE: Sum of elements on the list recursive - by sev - Jun-20-2019, 02:14 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  help with adding duplicates elements together in a list 2ECC3O 5 2,172 Sep-10-2022, 07:11 AM
Last Post: 2ECC3O
  Unexpected behavior accessing list elements. tonyflute 2 2,349 Apr-09-2021, 02:36 PM
Last Post: tonyflute
  How to find difference between elements in a list? Using beginner Basics only. Anklebiter 8 4,525 Nov-19-2020, 07:43 PM
Last Post: Anklebiter
  Get 5 most unique combinations of elements in a 2D list wanttolearn 1 2,368 Sep-24-2020, 02:26 PM
Last Post: buran
  Loop through elements of list and include as value in the dictionary Rupini 3 2,745 Jun-13-2020, 05:43 AM
Last Post: buran
  How can I print the number of unique elements in a list? AnOddGirl 5 3,396 Mar-24-2020, 05:47 AM
Last Post: AnOddGirl
  Help with Recursive solution,list items gianniskampanakis 8 3,771 Feb-28-2020, 03:36 PM
Last Post: gianniskampanakis
  Extracting elements in a list to form a message using for loop Tony04 2 2,437 Oct-25-2019, 05:55 PM
Last Post: ichabod801
  Recursive Function - Compare 2 lists, return the elements that don't exist in both KellyBaptist 1 5,322 Dec-23-2018, 10:10 AM
Last Post: Gribouillis
  Storing Minimum List of values from a recursive function sigsegv22 1 2,598 Sep-10-2018, 01:25 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020