Reputation: 47
So lets I have this list;
yy=[1,2,3,4,5,6,7,8,9,10]
and I want a map() to go through the list using the np.std() this,
np. std(1,2,3), std(2,3,4), std(3,4,5) .... std(8,9,10)
so I thought of doing something like this,
import numpy as np
y = [1,2,3,4,5,6,7,8,9,10]
n = 3
x = map(lambda w, n: np.std(y[x-n:x]), range(n,len(y)), n)
print(list(x))
But I get this error 'int' object is not iterable, how do I fix this?
Upvotes: 0
Views: 217
Reputation: 308111
OK, now I see what you were trying to do. You expected the single value of n
to be passed each time into your lambda function. But that's not how map
works, it expects all its arguments to be iterables.
You had another problem, you are passing w
into your lambda function but trying to use it as x
.
Here's the version of your code with those two problems fixed:
x = map(lambda w, n: np.std(y[w-n:w]), range(n,len(y)), [n]*len(y))
You can simplify it by realizing that you don't need to pass n
into the lambda function at all, it's already defined in the scope where you're creating the function.
x = map(lambda w: np.std(y[w-n:w]), range(n,len(y)))
Finally you can use a generator expression instead of map
.
x = (np.std(y[w-n:w]) for w in range(n,len(y)))
Upvotes: 0
Reputation: 21275
You're passing too many iterables to map
. You just need to pass the start index for each piece of the list that you want to np.std()
import numpy as np
y = [1,2,3,4,5,6,7,8,9,10]
n = 3
x = map(lambda i: np.std(y[i:i+n]), range(len(y) - n)) # pass only the start index for each iteration
print(list(x))
Result:
[0.816496580927726, 0.816496580927726, 0.816496580927726, 0.816496580927726, 0.816496580927726, 0.816496580927726, 0.816496580927726]
Upvotes: 3