在 Python 中将集合作为参数传递
我正在尝试在 python 中创建一个方法(并利用 numpy)来获取矩阵 A 并使用高斯消除法将其简化为行梯形形式。
我遇到了第一个问题,它试图将一组作为参数传递。
例如:def gauss([A]):
给我一个令我惊讶的错误。这是为什么呢?
另外,我计划在程序中构造矩阵的方式(也许这是唯一的方式?)如下:
a = numpy.array([[1 ,2 ,3] , [4 ,5 ,6] , [7 ,8 ,9]])
所以我希望能够将这个 a
传递到我的方法 gauss( a)
,然后让高斯对你的矩阵做可怕的事情,
谢谢
I am trying to create a method in python (and utilizing numpy) to take a matrix A
and reduce it to reduced row echelon form using the Gaussian Elimination Method.
I've run into my first problem which is trying to pass a set as a parameter.
For example:def gauss([A]):
gets me an error which was surprising to me. Why is this?
Also, the way I plan to construct matrices (maybe it's the only way?) in the program is as follows:
a = numpy.array([[1 ,2 ,3] , [4 ,5 ,6] , [7 ,8 ,9]])
So I would like to be able to pass this a
into my method gauss(a)
, and then have gauss do terrible things unto thy matrix
thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
JoshAdel 是对的(在现在已经离开的评论中),如果没有一些代码和示例输出,通常不可能判断发生了什么,但在这种情况下,我想我知道:
不是有效的 Python 语法。声明函数时,不能用方括号将参数名称括起来。 !
如果您想将矩阵/数组/嵌套列表或其他内容传递到函数中,只需使用即可
在该方法中,您可以将参数 A 视为矩阵。
JoshAdel is right (in the now dearly departed comment) that it's normally impossible to tell what's going on without some code and sample output, but in this case I think I know:
is not valid Python syntax. When you're declaring a function, you can't surround the parameter names with brackets. Just use
And if you want to pass a matrix/array/nested list or whatever into the function, just do it!
In the method you can then treat the parameter
A
as a matrix.如果您不熟悉 Python 和 Python 中的函数定义,可以了解一些更多信息。
[A]
(即括在方括号中的A
)是文字表达式,用于创建包含A
。正如42
是“生成”整数 42 的文字表达式一样,'foobar' 是“生成”字符串foobar
的文字表达式因此,尝试使用
def gauss([A]): pass
定义一个函数,就像尝试定义一个类似def gauss(42): pass
的函数一样没有意义。代码> 不会 感觉。然而 def gauss(a=20): pass 则完全是另一回事了。在本例中,它创建一个带有参数
a
且默认值为 20 的函数。附带说明一下,不建议定义像def gauss( a=[]): pass
因为[]
是可变的,并且作为默认参数会导致问题。正如其他人已经建议的那样,
def gauss(a): ...
将修复您的错误。Some further information in case you are new to Python and function definitions in Python.
[A]
(i.eA
enclosed in square brackets) is the literal expression for making a list containingA
. Just as42
is the literal expression for "making" an integer 42, 'foobar' is the literal expression for "making" the stringfoobar
etc.So trying to define a function with
def gauss([A]): pass
wouldn't make sense just as trying to define a function likedef gauss(42): pass
woudn't make sense.However
def gauss(a=20): pass
is whole another story. In this case it creates a function with argumenta
and default value of 20. As a side note, it is not recommended to define a function likedef gauss(a=[]): pass
because[]
is mutable and will cause issues as a default argument.As others have already suggested
def gauss(a): ...
will fix your errors.def func(param):
是定义函数的方式。在函数定义期间,您不会“传递”任何内容。这有效吗?def func(param):
is how you define a function. You don't "pass" anything during a function definition. Does this work?