python: 'numpy.int32' object is not iterable when using reduce on a numpy list

Viewed 592

I am new to python programming. when using reduce() function on a numpy array (like this code):

import numpy as np
from functools import reduce
x = np.arange(1, 5)    
y = list(reduce(lambda s, t: s + t, x))

I receive this error:

TypeError: 'numpy.int32' object is not iterable

I tested the same codes for map() function and had no error:

import numpy as np
x = np.arange(1, 5)    
z = list(map(lambda s: s if s%2 == 0  else None, x))

How can I use reduce function for a numpy array? Thanks in advance.

1 Answers

I found my fault!

reduce() function a single value not a list. So this code,

y = reduce(lambda s, t: s + t, x)

has no error.

Related