Python Chapter 7 Exercises plus (1)

Improve the function fun1. The parameter data is an integer list. The even numbers in data are extracted and stored in an array. Numpy must be used, and the return result is Numpy's ndarray type.

Just find an even index

def fun1(data=[1,2,3,4,5,6,7,8,9,10]):
    """
    Arg:
        data : a list as input; e.g. [1,2,3,4,5,6,7,8,9,10]
    return a Numpy ndarray; e.g. [ 2  4  6  8 10]
    """
    data1=np.array(data)
    index=np.where(data1%2==0)
    return data1[index]

Guess you like

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