简体   繁体   English

使用一个函数返回多个函数的输出

[英]return Outputs of multiple function using one function

I have a fully functional code with three functions each of which returns outputs of it own. 我有一个功能完整的代码,其中包含三个函数,每个函数返回其自己的输出。 My task is to create one function which can call all the other functions and return the outputs of all the functions. 我的任务是创建一个可以调用所有其他功能并返回所有功能输出的功能。 how do I do that? 我怎么做? My code is as follows: 我的代码如下:

def func(x, y):
    return x * x + y * y, x * y

def generate_data(size):
    nx = 5
    mx = 0.5
    mux, sigmax = nx, mx/3
    ny = 3
    my = 0.9
    muy, sigmay = ny, my/3
    result1=[]
    result2=[]
    for i in range(size):
        result = func(random.gauss(mux, sigmax), random.gauss(muy, sigmay))
        result1.append(result[0])
        result2.append(result[1])
    return result1, result2

def analysis(ls):

    avg = np.mean(ls)
    std = np.std(ls)
    pre = 3 * std


    return avg, std, pre

say, for eg: I need to create a function 例如说:我需要创建一个函数

def My_func()

and this function should have the other functions and returns its outputs 该函数应具有其他函数并返回其输出

Just as you have done in each of your existing functions, you can return multiple results in one line. 就像在每个现有函数中所做的一样,您可以在一行中返回多个结果。

def My_func(x, y, size, ls):
    return analysis(ls), generate_data(size), func(x,y)

You can return them at once.Like this: 您可以一次将它们退回。

return analysis(x,y), generate_data(size), func(ls)

To receive the value, you have to unpack 要获得价值,您必须打开包装

Valanalysis,Valgenerate_data,valfunc=My_func(x, y,size,ls)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM