0

I have a function that will run a couple of times (in a recursive function, so with a 'while' condition). Everytime it runs, it will return a certain integer. So for example, the first time it runs it returns a 3, the second time a 5 and the third time a 9.

Now I need to save these returns in a list. So I thought to create a separate function that would take these values and store them. So the endstate I'm looking for is to have a list = [3,5,8].

B = [3,6,5,7,8,10]
def function_1(A):
    for i in range(len(A)/2):
         factor = A[2*i]
         list_of_diagonals(factor)
    return factor`

def list_of_diagonals(d):
    factor_list = []
    factor_list = factor_list.append(d)
    return factor_list`

Now I would expect that print function_1(B) would produces [3,5,8] but instead it just produces 8.

I think it has something to do with the fact that I define factor_list=[] right at the start of the function, but how could I work around that?

9
  • You are just returning factor from function_1 which holds the last iterated value of A[2*i] Commented Jan 17, 2019 at 6:35
  • Have you tried using yield yet? Commented Jan 17, 2019 at 6:35
  • I don't think "recursive" is the same as a "while" condition. You also don't have a while statement in your code anywhere. Just a for loop. Commented Jan 17, 2019 at 6:37
  • I'm not familiar with yield. But I return factor in the first function, just to make sure I could use it in the second. I return factor_list in the second, so I could use it later, right? Commented Jan 17, 2019 at 6:37
  • 1
    @SpghttCd The function has to be defined before it's called, not before it appears in another definition. Python just looks up the name in the globals dict at runtime. I don't see any problem with the order of the functions. Commented Jan 17, 2019 at 6:41

4 Answers 4

3

using generator you can do this this way , better , readable and more pythonic

B = [3,6,5,7,8,10]
def function_1(A):
    for i in range(len(A)//2):
         factor = A[2*i]
         yield factor

result = list(function_1(B))
# output [3, 5, 8]
Sign up to request clarification or add additional context in comments.

1 Comment

@Mathbeginner that's the yield I was talking about.
1

you are creating empty factor_list every time whenever list of diagonals call happens. Actually you don't need another function to store result in list , try below code:

B = [3,6,5,7,8,10]
def function_1(A):
    l1 = []
    for i in range(len(A)/2):
         factor = A[2*i]
         l1.append(factor)
    return l1
print function_1(B)

6 Comments

have you checked before posting. This doesn't seem to work.
@VineethSai what is not working here can you explain? Yes.. it is working I am getting desired output [3, 5, 8]
Maybe it's the issue with range being a generator in python3.x
TypeError: 'float' object cannot be interpreted as an integer
Strange.. At my end its working fine.. I am using python ... Are you using same B list?
|
1

Other users already pointed the option to use a list directly in the first method, but I'd suggest one way to refactor your code to make it work.

def function_1(array):
    list_of_diagonals_ = [] # <-- initialize the recipient
    for i in range(len(array)//2): # <-- # // to return integer
         factor = array[2*i]
         list_of_diagonals_ = list_of_diagonals(factor, list_of_diagonals_) # <-- call the function and store to recipient
    return list_of_diagonals_ # <-- return the recipient

def list_of_diagonals(element, factor_list = None): # <-- need an argument to memorize past results
    if factor_list == None: factor_list = [] # <-- just in case
    factor_list.append(element)
    return factor_list

B = [3,6,5,7,8,10]
print (function_1(B))
#=> [3, 5, 8]

1 Comment

NIce one, I appreciate it and it's definitely useful for my understanding.
0

The following codes is under Python2.7

using islice in itertools can handle your case

from itertools import islice

B = [3, 6, 5, 7, 8, 10]


def function_1(A):
    return islice(A, 0, None, 2)

# [3, 5, 8]
print(list(function_1(B)))

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.