简体   繁体   English

Python 函数只输出一个结果,而不是采用不同的输入

[英]Python function only outputs one result instead of taking different inputs

I am trying to build a Python function that accepts numerous inputs (indefinite) in a list like this:我正在尝试构建一个 Python 函数,该函数在如下列表中接受大量输入(不确定):

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但它只输出一个结果而不是列表中的三个输出: 2.0248456731316713

What did I do wrong with the code above?上面的代码我做错了什么? Thanks!谢谢!

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. return语句在第一次迭代时停止函数执行,返回单个值,即输入列表第一项的计算值。

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.您正在寻找的是一个generator ,它会在每次调用该函数时返回一个新值,或者是一个list comprehension ,它将返回一个包含计算值的新列表。

You may also use numpy arrays directly as you seem to have it as a dependency (thanks @GIRISH kuniyal for the idea).您也可以直接使用 numpy 数组,因为您似乎将其作为依赖项(感谢@GIRISH kuniyal的想法)。

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]

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]

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.使用 numpy 数组可以轻松实现这一点,并且与 python 列表相比,它的速度也很快。

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.还要注意代码中的最初问题是您在错误的地方使用了 return ,因为它在第一次迭代时将控制返回到调用行。

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你也可以在这里使用列表推导,这是一种更加 Pythonic 的方法,请看下面的代码

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)

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

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