The seventh chapter of python exercises (10)

Improve the function fun10, the parameter data is an integer list, replace the odd data in data with 1, and the even number with -1.
You must use numpy, and the return result is Numpy's ndarray type.

Tip: np.where(condition, x, y)
pay attention to the usage of where

def fun10(data=[9,2,6,4,2,6,7,8,2,10]):
    """
    Arg:
        data : an array as input; e.g. array([9,2,6,4,2,6,7,8,2,10])
    return a Numpy ndarray; e.g. [ 1 -1 -1 -1 -1 -1  1 -1 -1 -1]
    """
    nup=np.array(data)
    return np.where(nup%2!=0,1,-1)
    pass

Guess you like

Origin blog.csdn.net/qq_53029299/article/details/115117798