Reputation: 39
I am given an array X and I am asked to return another array Y where y[t] = x[t] + x[t-1] and y[0] = 0 without using a for loop in Python.
What I can think of is using rolling sum but I am not sure if this is the fastest way since I need to convert x into a dataframe/ a series for rolling to work. Is there a faster way to do that? Thanks!
df = pd.DataFrame(X).rename(columns={0: 'X'})
df['Y'] = df['X'].rolling(2).sum().fillna(0)
Y = df['Y'].values
Upvotes: 0
Views: 1135
Reputation: 9868
Well, you could use numpy
, though that still turns the list into an array.
y = x + np.roll(x, 1)
y[0] = 0
This is fast, short, fairly transparent and doesn't (explicitly) use a for
loop.
You could also use map
, which is pretty much the same as the list comprehension and doesn't require any external libraries.
y = 0 + map(lambda (a, b): a+b, zip(x, x[1:]))
In Python3 this doesn't work, and instead you would need to write:
y = [0] + list(map(lambda a: a[0]+a[1], zip(x, x[1:])))
or
y = [0] + list(map(sum, zip(x, x[1:])))
Upvotes: 0
Reputation: 8273
In case you are looking for one-liners might not give best performace
from toolz.itertoolz import sliding_window
[0]+map(sum,list(sliding_window(2, [1, 2, 3, 4]))) # [0, 3, 5, 7]
Upvotes: 1
Reputation: 61930
You could use a list comprehension and zip:
x = [1, 2, 3, 4]
y = [0] + [c + n for c, n in zip(x, x[1:])]
print(y)
Output
[0, 3, 5, 7]
This approach relies on built-in functions, so no need to import an external module such as pandas.
Upvotes: 1