Skip to main content
added 54 characters in body
Source Link
Graipher
  • 41.7k
  • 7
  • 70
  • 134

Note that Python's official style-guide, PEP8, recommends using lower_case names for variables, so I changed all your Ls to l and all your new_L to new_l.

l = [4, 2, 1, 3]

You should keep track of the cumulative sum in a variable. This way you avoid having to test whether the new_l already has an element in it:

def add_one_by_one(l):
    new_l = []
    cumsum = 0
    for elt in l:
        cumsum += elt
        new_l.append(cumsum)
    return new_l

As an alternative, you could use a generator to avoid having to build the list inside the function (if you are just going to iterate over it later, this is the most memory-efficient way):

def add_one_by_one_gen(l):
    cumsum = 0
    for elt in l:
        cumsum += elt
        yield cumsum

new_l = list(add_one_by_one_gen(l))

# This takes basically no additional memory (only one float/int):
for x in add_one_by_one_gen(l):
    print x

(Replace print x with print(x) in Python 3.x.)

Probably the fastest way to do it would be using the numpy function cumsum:

import numpy as np

new_l = np.cumsum(l)

Note that Python's official style-guide, PEP8, recommends using lower_case names for variables, so I changed all your Ls to l and all your new_L to new_l.

l = [4, 2, 1, 3]

You should keep track of the cumulative sum in a variable. This way you avoid having to test whether the new_l already has an element in it:

def add_one_by_one(l):
    new_l = []
    cumsum = 0
    for elt in l:
        cumsum += elt
        new_l.append(cumsum)
    return new_l

As an alternative, you could use a generator to avoid having to build the list inside the function (if you are just going to iterate over it later, this is the most memory-efficient way):

def add_one_by_one_gen(l):
    cumsum = 0
    for elt in l:
        cumsum += elt
        yield cumsum

new_l = list(add_one_by_one_gen(l))

# This takes basically no additional memory (only one float/int):
for x in add_one_by_one_gen(l):
    print x

Probably the fastest way to do it would be using the numpy function cumsum:

import numpy as np

new_l = np.cumsum(l)

Note that Python's official style-guide, PEP8, recommends using lower_case names for variables, so I changed all your Ls to l and all your new_L to new_l.

l = [4, 2, 1, 3]

You should keep track of the cumulative sum in a variable. This way you avoid having to test whether the new_l already has an element in it:

def add_one_by_one(l):
    new_l = []
    cumsum = 0
    for elt in l:
        cumsum += elt
        new_l.append(cumsum)
    return new_l

As an alternative, you could use a generator to avoid having to build the list inside the function (if you are just going to iterate over it later, this is the most memory-efficient way):

def add_one_by_one_gen(l):
    cumsum = 0
    for elt in l:
        cumsum += elt
        yield cumsum

new_l = list(add_one_by_one_gen(l))

# This takes basically no additional memory (only one float/int):
for x in add_one_by_one_gen(l):
    print x

(Replace print x with print(x) in Python 3.x.)

Probably the fastest way to do it would be using the numpy function cumsum:

import numpy as np

new_l = np.cumsum(l)
added 84 characters in body
Source Link
Graipher
  • 41.7k
  • 7
  • 70
  • 134

WellNote that Python's official style-guide, probably the fastest way to do it would bePEP8, recommends using the numpylower_case functionnames for variables, so I changed all your cumsumL:s to l and all your new_L to new_l.

import numpy as np

l = [4, 2, 1, 3]
new_l = np.cumsum(l)

Another possibility, if you want to do it yourself, is toYou should keep track of the cumulative sum in a variable. This way you avoid having to test whether the new_l already has an element in it:

def add_one_by_one(l):
    new_l = []
    cumsum = 0
    for elt in l:
        cumsum += elt
        new_l.append(cumsum)
    return new_l

As a thirdan alternative, you could use a generator to avoid having to build the list inside the function (if you are just going to iterate over it later, this is the most memory-efficientmemory-efficient way):

def add_one_by_one_gen(l):
    cumsum = 0
    for elt in l:
        cumsum += elt
        yield cumsum

new_l = list(add_one_by_one_gen(l)) 

# This takes basically no additional memory (only one float/int):
for x in add_one_by_one_gen(l):
    print x

Note that Python's official style-guide, PEP8, recommends using lower_case names for variables, so I changed all yourProbably the Lsfastest way to l and all yourdo it would be using the new_Lnumpy tofunction new_l.cumsum:

import numpy as np

new_l = np.cumsum(l)

Well, probably the fastest way to do it would be using the numpy function cumsum:

import numpy as np

l = [4, 2, 1, 3]
new_l = np.cumsum(l)

Another possibility, if you want to do it yourself, is to keep track of the sum in a variable:

def add_one_by_one(l):
    new_l = []
    cumsum = 0
    for elt in l:
        cumsum += elt
        new_l.append(cumsum)
    return new_l

As a third alternative, you could use a generator to avoid having to build the list inside the function (if you are just going to iterate over it later, this is the most memory-efficient way):

def add_one_by_one_gen(l):
    cumsum = 0
    for elt in l:
        cumsum += elt
        yield cumsum

new_l = list(add_one_by_one_gen(l))

for x in add_one_by_one_gen(l):
    print x

Note that Python's official style-guide, PEP8, recommends using lower_case names for variables, so I changed all your Ls to l and all your new_L to new_l.

Note that Python's official style-guide, PEP8, recommends using lower_case names for variables, so I changed all your Ls to l and all your new_L to new_l.

l = [4, 2, 1, 3]

You should keep track of the cumulative sum in a variable. This way you avoid having to test whether the new_l already has an element in it:

def add_one_by_one(l):
    new_l = []
    cumsum = 0
    for elt in l:
        cumsum += elt
        new_l.append(cumsum)
    return new_l

As an alternative, you could use a generator to avoid having to build the list inside the function (if you are just going to iterate over it later, this is the most memory-efficient way):

def add_one_by_one_gen(l):
    cumsum = 0
    for elt in l:
        cumsum += elt
        yield cumsum

new_l = list(add_one_by_one_gen(l)) 

# This takes basically no additional memory (only one float/int):
for x in add_one_by_one_gen(l):
    print x

Probably the fastest way to do it would be using the numpy function cumsum:

import numpy as np

new_l = np.cumsum(l)
added 56 characters in body
Source Link
Graipher
  • 41.7k
  • 7
  • 70
  • 134

Well, probably the fastest way to do it would be using the numpy function cumsum:

import numpy as np

l = [4, 2, 1, 3]
new_l = np.cumsum(l)

Another possibility, if you want to do it yourself, is to keep track of the sum in a variable:

def add_one_by_one(l):
    new_l = []
    cumsum = 0
    for elt in l:
        cumsum += elt
        new_l.append(cumsum)
    return new_l

As a third alternative, you could use a generator to avoid having to build the list inside the function (if you are just going to iterate over it later, this is the most memory-efficient way):

def add_one_by_one_gen(l):
    cumsum = 0
    for elt in l:
        cumsum += elt
        yield cumsum

new_l = list(add_one_by_one_gen(l)) 

for x in add_one_by_one_gen(l):
    print x

Note that Python's official style-guide, PEP8, recommends using lower_case names for variables, so I changed all your Ls to l and all your new_L to new_l.

Well, probably the fastest way to do it would be using the numpy function cumsum:

import numpy as np

l = [4, 2, 1, 3]
new_l = np.cumsum(l)

Another possibility, if you want to do it yourself, is to keep track of the sum in a variable:

def add_one_by_one(l):
    new_l = []
    cumsum = 0
    for elt in l:
        cumsum += elt
        new_l.append(cumsum)
    return new_l

As a third alternative, you could use a generator to avoid having to build the list inside the function (if you are just going to iterate over it later, this is the most memory-efficient way):

def add_one_by_one_gen(l):
    cumsum = 0
    for elt in l:
        cumsum += elt
        yield cumsum

new_l = list(add_one_by_one_gen(l))

Note that Python's official style-guide, PEP8, recommends using lower_case names for variables, so I changed all your Ls to l and all your new_L to new_l.

Well, probably the fastest way to do it would be using the numpy function cumsum:

import numpy as np

l = [4, 2, 1, 3]
new_l = np.cumsum(l)

Another possibility, if you want to do it yourself, is to keep track of the sum in a variable:

def add_one_by_one(l):
    new_l = []
    cumsum = 0
    for elt in l:
        cumsum += elt
        new_l.append(cumsum)
    return new_l

As a third alternative, you could use a generator to avoid having to build the list inside the function (if you are just going to iterate over it later, this is the most memory-efficient way):

def add_one_by_one_gen(l):
    cumsum = 0
    for elt in l:
        cumsum += elt
        yield cumsum

new_l = list(add_one_by_one_gen(l)) 

for x in add_one_by_one_gen(l):
    print x

Note that Python's official style-guide, PEP8, recommends using lower_case names for variables, so I changed all your Ls to l and all your new_L to new_l.

Source Link
Graipher
  • 41.7k
  • 7
  • 70
  • 134
Loading