将数组/列表传递给 Python 函数

发布于 2024-09-27 14:59:23 字数 347 浏览 5 评论 0原文

我一直在考虑将数组或列表(Python 倾向于这样称呼它们)传递到函数中。

我读到了一些关于使用 *args 的内容,例如:

def someFunc(*args)
    for x in args
        print x

但不确定这是否正确/错误。似乎没有什么能如我所愿。我习惯于能够轻松地将数组传递到 PHP 函数中,但这让我感到困惑。我似乎也不能这样做:

def someFunc(*args, someString)

因为它会抛出错误。

我想我只是把自己完全搞糊涂了,想找人来帮我解决这个问题。

I've been looking at passing arrays, or lists, as Python tends to call them, into a function.

I read something about using *args, such as:

def someFunc(*args)
    for x in args
        print x

But not sure if this is right/wrong. Nothing seems to work as I want. I'm used to be able to pass arrays into PHP function with ease and this is confusing me. It also seems I can't do this:

def someFunc(*args, someString)

As it throws up an error.

I think I've just got myself completely confused and looking for someone to clear it up for me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

梦里°也失望 2024-10-04 14:59:23

当您使用以下语法定义函数时:

def someFunc(*args):
    for x in args
        print x

您告诉它您需要可变数量的参数。如果你想传入一个列表(来自其他语言的数组),你可以这样做:

def someFunc(myList = [], *args):
    for x in myList:
        print x

然后你可以这样调用它:

items = [1,2,3,4,5]

someFunc(items)

你需要在变量参数之前定义命名参数,在关键字参数之前定义变量参数。您还可以这样:

def someFunc(arg1, arg2, arg3, *args, **kwargs):
    for x in args
        print x

它需要至少三个参数,并支持可变数量的其他参数和关键字参数。

When you define your function using this syntax:

def someFunc(*args):
    for x in args
        print x

You're telling it that you expect a variable number of arguments. If you want to pass in a List (Array from other languages) you'd do something like this:

def someFunc(myList = [], *args):
    for x in myList:
        print x

Then you can call it with this:

items = [1,2,3,4,5]

someFunc(items)

You need to define named arguments before variable arguments, and variable arguments before keyword arguments. You can also have this:

def someFunc(arg1, arg2, arg3, *args, **kwargs):
    for x in args
        print x

Which requires at least three arguments, and supports variable numbers of other arguments and keyword arguments.

自演自醉 2024-10-04 14:59:23

您可以像其他类型一样传递列表:

l = [1,2,3]

def stuff(a):
   for x in a:
      print a


stuff(l)

这将打印列表 l。请记住,列表作为引用传递,而不是作为深层副本传递。

You can pass lists just like other types:

l = [1,2,3]

def stuff(a):
   for x in a:
      print a


stuff(l)

This prints the list l. Keep in mind lists are passed as references not as a deep copy.

┈┾☆殇 2024-10-04 14:59:23

您不需要使用星号来接受列表。

只需在定义中为参数指定一个名称,然后传入一个列表,例如

def takes_list(a_list):
    for item in a_list:
         print item

You don't need to use the asterisk to accept a list.

Simply give the argument a name in the definition, and pass in a list like

def takes_list(a_list):
    for item in a_list:
         print item
梦中的蝴蝶 2024-10-04 14:59:23

Python 列表(不仅仅是数组,因为它们的大小可以动态更改)是普通的 Python 对象,可以作为任何变量传递给函数。 * 语法用于解包列表,这可能不是您现在想要做的事情。

Python lists (which are not just arrays because their size can be changed on the fly) are normal Python objects and can be passed in to functions as any variable. The * syntax is used for unpacking lists, which is probably not something you want to do now.

没有伤那来痛 2024-10-04 14:59:23
def sumlist(items=[]):
    sum = 0
    for i in items:
        sum += i

    return sum

t=sumlist([2,4,8,1])
print(t)
def sumlist(items=[]):
    sum = 0
    for i in items:
        sum += i

    return sum

t=sumlist([2,4,8,1])
print(t)
不弃不离 2024-10-04 14:59:23

有一种方法可以直接传递列表而不是函数参数。在这种情况下,您的函数具有固定数量的参数,希望将这些参数打包到列表中并使用您的列表调用该函数。

函数scipy.optimize.curve_fit利用这个,因为它期望函数由先验未知数量的值参数化,例如,您可以指定

import numpy as np
def f(x, a0, a1, a2):
   return a0+a1*np.exp(a2*x)

让一个函数由三个参数参数化。然后调用

fit, cov = curve_fit(f, xdata, ydata)

,函数返回与参数数量对应的长度的拟合列表,有趣的是,您可以获取通过fit参数化的函数的值v通过调用 x 处的参数

fit=[1,2,3]
x=0
v = f(x, *fit)

参见 scipy 来源

There is a way how to pass list directly instead of function params. In this scenario you have function with fixed number of parameters, would like to pack these into the list and call the function using your list.

The function scipy.optimize.curve_fit utilizes this as it expect the function parametrized by apriori unknown number of values, e.g. you can specify

import numpy as np
def f(x, a0, a1, a2):
   return a0+a1*np.exp(a2*x)

To have a function parametrized by three parameters. Then you call

fit, cov = curve_fit(f, xdata, ydata)

and the function returns fit list of the length corresponding to the number of parameters, interestingly you can then obtain value v of function parametrized by fit params at the point x by calling

fit=[1,2,3]
x=0
v = f(x, *fit)

See scipy sources

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文