0

I have a function:

def function(x,y):
    do something
    print a,b
    return a,b

Now I use a for loop like:

for i in range(10,100,10):
    function(i,30)

which prints the values a,b for the given input values via the for loop. It also returns a,b if I say for example function(10,30) like:

Out[50]: (0.25725063633960099, 0.0039189363571677958)

I would like to append the values of a,b obtained for my different input parameters (x,y) via the for loop to two empty lists.

I tried

for i in range(10,100,10):
    list_a,list_b = function(i,30)

but list_a and list_b are still empty.

EDIT:

I have also tried:

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])

But list_a and list_b are empty!

What I don't understand is that, when I call

function(10,30)[0]

for instance, it outputs a value! But why am I not able to append it to a list?

Here is the entire function as asked by a few.

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 
8
  • You are setting the lists on each iteration to the return values of the function. I believe you are trying to .append(val). Commented Sep 8, 2016 at 14:14
  • @TheLazyScripter: Can you be more specific? Commented Sep 8, 2016 at 14:22
  • 1
    Please post entire code, every answer below will and does work. The only other explanation would be an error elsewhere. Commented Sep 8, 2016 at 14:35
  • Yes @TheLazyScripter. I think the issue must lie within the function, but it seems very strange if all these answers result in empty lists. Even if the function returned None or an empty string, these will still be appended to the lists. Commented Sep 8, 2016 at 14:41
  • @TheLazyScripter: please have a look at the entire function. It returns the two values as a tuple, i.e. (0.16552264676265405, 0.011976798379824433) Commented Sep 8, 2016 at 14:45

4 Answers 4

5

you can use list comprehension first, get list_a, list_b via zip.

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

result = [function(i,30) for i in range(10,100,10)]
list_a, list_b = zip(*result)
Sign up to request clarification or add additional context in comments.

1 Comment

list_a and list_b are still empty!!
1

Something like this should work:

# Define a simple test function
def function_test(x,y): 
    return x,y

# Initialize two empty lists
list_a = []
list_b = []
# Loop over a range
for i in range(10,100,10):
        a = function_test(i,30) # The output of the function is a tuple, which we put in "a"
        # Append the output of the function to the lists
        # We access each element of the output tuple "a" via indices
        list_a.append(a[0])
        list_b.append(a[1])
# Print the final lists      
print(list_a)
print(list_b)

2 Comments

Can you please add some explenation to your answer? Only showing code can be confusing for some people.
Thanks @AndréKool , I added some comments to the code :)
0

You mean something like that:

list_a = []
list_b = []

for i in range(10,100,10):
    a, b = function(i,30)
    list_a.append(a)
    list_b.append(b)

Comments

-2

you may need to try map() function, which is more friendly~~

Understanding the map function

which should be the same as in python 3: def map(func, iterable): for i in iterable: yield func(i)

under python 2 map will return the full list

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.