I am trying to convert this piece of code into a list comprehension:
a = np.random.rand(10) #input vector
n = len(a) # element count of input vector
b = np.random.rand(3) #coefficient vector
nb = len(b) #element count of coefficients
d = nb #decimation factor (could be any integer < len(a))
c = []
for i in range(0, n, d):
psum = 0
for j in range(nb):
if i + j < n:
psum += a[i + j]*b[j]
c.append(psum)
I've tried following suggestions from:
- List comprehension with an accumulator
- nested for loops to list comprehension with differents "if" conditions
For example:
from itertools import accumulate
c = [accumulate([a[i + j] * b[j] for j in range(nb) if i + j < n] ) for i in range(0, n, d)]
Later, when trying to get values from c
(e.g. c[:index]
):
TypeError: 'NoneType' object is not subscriptable
Or:
from functools import partial
def get_val(a, b, i, j, n):
if i + j < n:
return(a[i + j] * b[j])
else:
return(0)
c = [
list(map(partial(get_val, i=i, j=j, n=n), a, b))
for i in range(0, n, d)
for j in range(nb)
]
in get_val
, return(a[i + j] * b[j])
IndexError: invalid index to scalar variable.
Or:
psum_pieces = [[a[i + j] * b[j] if i + j < n else 0 for j in range(nb)] for i in range(0, n, d)]
c = [sum(psum) for psum in psum_pieces]
As well as many other iterations of these approaches. Any guidance would be much appreciated.