将数组/列表传递给 Python 函数
我一直在考虑将数组或列表(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
当您使用以下语法定义函数时:
您告诉它您需要可变数量的参数。如果你想传入一个列表(来自其他语言的数组),你可以这样做:
然后你可以这样调用它:
你需要在变量参数之前定义命名参数,在关键字参数之前定义变量参数。您还可以这样:
它需要至少三个参数,并支持可变数量的其他参数和关键字参数。
When you define your function using this syntax:
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:
Then you can call it with this:
You need to define named arguments before variable arguments, and variable arguments before keyword arguments. You can also have this:
Which requires at least three arguments, and supports variable numbers of other arguments and keyword arguments.
您可以像其他类型一样传递列表:
这将打印列表 l。请记住,列表作为引用传递,而不是作为深层副本传递。
You can pass lists just like other types:
This prints the list l. Keep in mind lists are passed as references not as a deep copy.
您不需要使用星号来接受列表。
只需在定义中为参数指定一个名称,然后传入一个列表,例如
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
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.
有一种方法可以直接传递列表而不是函数参数。在这种情况下,您的函数具有固定数量的参数,希望将这些参数打包到列表中并使用您的列表调用该函数。
函数
scipy.optimize.curve_fit
利用这个,因为它期望函数由先验未知数量的值参数化,例如,您可以指定让一个函数由三个参数参数化。然后调用
fit, cov = curve_fit(f, xdata, ydata)
,函数返回与参数数量对应的长度的拟合列表,有趣的是,您可以获取通过fit参数化的函数的值v通过调用 x 处的参数
参见 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 specifyTo 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
See scipy sources