jeudi 17 mai 2018

Python, how could i define function when the parameter is not given?

Python masters~, i have a question. how could i define function, the situation one of the parameter is not given.

for example, i want to create standard deviation function which is calculated by given length. so when the parameter length is not given, i want to return the standard deviation in total length of the source, so just one value.

def std_series(source, length):
        """standard deviation in given length"""
        if length == 1:
            return source
        elif not length:
            return np.std(source)
        else:
            for i in range(length - 1):
                i = np.nan
                yield i
            for i in range(len(source) - length+1):
                std = np.std(source[i : i+length])
                yield std

so the problem is here,

    elif not length:
        return np.std(source)

this works not. how could i create a branch which express when length is not given?

Aucun commentaire:

Enregistrer un commentaire