Python 通过 for 循环将值附加到从函数返回的列表中

The*_*tor 0 python for-loop function list append

我有一个功能:

def function(x,y):
    do something
    print a,b
    return a,b
Run Code Online (Sandbox Code Playgroud)

现在我使用 for 循环,例如:

for i in range(10,100,10):
    function(i,30)
Run Code Online (Sandbox Code Playgroud)

a,b它通过 for 循环打印给定输入值的值。a,b如果我说,它也会返回,function(10,30)例如:

Out[50]: (0.25725063633960099, 0.0039189363571677958)
Run Code Online (Sandbox Code Playgroud)

我想将通过 for 循环a,b为不同输入参数获得的值附加到两个空列表。(x,y)

我试过

for i in range(10,100,10):
    list_a,list_b = function(i,30)
Run Code Online (Sandbox Code Playgroud)

list_alist_b仍然是空的。

编辑:

我也尝试过:

list_a = []
list_b = []
for i in range(10,100,10):
    list_a.append(function(i,30)[0])
    list_b.append(function(i,30)[1])
Run Code Online (Sandbox Code Playgroud)

list_alist_b都是空的!

我不明白的是,当我打电话时

function(10,30)[0]

例如,它输出一个值!但为什么我无法将其附加到列表中?

这是一些人询问的完整功能。

def function(N,bins):
    sample = np.log10(m200_1[n200_1>N]) # can be any 1D array
    mean,scatter = stats.norm.fit(sample) #Gives the paramters of the fit to the histogram
    err_std = scatter/np.sqrt(len(sample))

    if N<30:
        x_fit = np.linspace(sample.min(),sample.max(),100)
        pdf_fitted = stats.norm.pdf(x_fit,loc=mean,scale=scatter) #Gives the PDF, given the parameters from norm.fit
        print "scatter for N>%s is %s" %(N,scatter)
        print "error on scatter for N>%s is %s" %(N,err_std)
        print "mean for N>%s is %s" %(N,mean)  

    else:
        x_fit = np.linspace(sample.min(),sample.max(),100)
        pdf_fitted = stats.norm.pdf(x_fit,loc=mean,scale=scatter) #Gives the PDF, given the parameters from norm.fit
        print "scatter for N>%s is %s" %(N,scatter) 
        print "error on scatter for N>%s is %s" %(N,err_std)
        print "mean for N>%s is %s" %(N,mean)

    return scatter,err_std 
Run Code Online (Sandbox Code Playgroud)

Phi*_*ang 5

你可以先使用列表理解,通过zip获取list_a,list_b。

def function(x,y):
    return x,y

result = [function(i,30) for i in range(10,100,10)]
list_a, list_b = zip(*result)
Run Code Online (Sandbox Code Playgroud)