0

I am trying to build a Python function that accepts numerous inputs (indefinite) in a list like this:

def limit(x_es):
    for x in x_es:
        return np.sqrt((3-5*x + x**2 + x**3)) / (x-1)


numbers= [1.1, 1.01, 1.001]
limit(numbers)

But it only outputs one result instead of three outputs from the list: 2.0248456731316713

What did I do wrong with the code above? Thanks!

4 Answers 4

2

The return statement stops the function execution at the first iteration, returning a single value, the computed value for the first item of the input list.

What you are looking for is either a generator, which will return a new value, each time you call the function, or a list comprehension, which will return a new list with the computed values.

You may also use numpy arrays directly as you seem to have it as a dependency (thanks @GIRISH kuniyal for the idea).

import numpy as np

# Generator
def limit_generator(x_es):
    for x in x_es:
        yield np.sqrt((3-5*x+x**2+x**3))/(x-1)

# List comprehension
def limits(x_es):
    return [np.sqrt((3-5*x+x**2+x**3))/(x-1) for x in x_es]

# Numpy arrays
def numpy_limits(x_es):
    x = np.array(x_es)
    return np.sqrt((3-5*x+x**2+x**3))/(x-1)

if __name__ == '__main__':
    numbers = [1.1, 1.01, 1.001]

    generator = limit_generator(numbers)
    print(next(generator), next(generator), next(generator))

    print(limits(numbers))

    print(numpy_limits(numbers))
2.0248456731316713 2.00249843945087 2.000249984482112
[2.0248456731316713, 2.00249843945087, 2.000249984482112]
[2.02484567 2.00249844 2.00024998]
Sign up to request clarification or add additional context in comments.

Comments

1

You are returning the first element, fix it by appending to a list instead

def limit(x_es):
    result = []
    for x in x_es:
        result.append(np.sqrt((3-5*x+x**2+x**3))/(x-1))
    return result   

numbers= [1.1, 1.01,1.001]        
limit(numbers)
#[2.0248456731316713, 2.00249843945087, 2.000249984482112]

Comments

1

Hope This code may help you.

   def limit(x_es):
        numbers = np.array(x_es)
        return np.sqrt(3-(5*numbers)+(numbers**2)+(numbers**3))/(numbers-1)

   numbers= [1.1, 1.01,1.001]        
   limit(numbers)

This is easily accomplished using numpy array and its fast too as compare to python list.

Comments

0

You could use below code ,which fixes the issue

Also note the initial issue in your code is that you are using return in the wrong place as it returns the control to the calling line at first iteration itself.

import numpy as np

def limit(x_es):
    result = []
    for x in x_es:
        result.append(np.sqrt((3-5*x+x**2+x**3))/(x-1))
    return result


if __name__ == '__main__':
    numbers= [1.1, 1.01,1.001]
    limit(numbers)

You could also use list comprehension here , which is a more pythonic way of doing this , look at below code

import numpy as np

if __name__ == '__main__':
    numbers= [1.1, 1.01,1.001]
    #print(limit(numbers))
    list = []
    list = [np.sqrt((3-5*x+x**2+x**3))/(x-1) for x in numbers ]
    print(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.