Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将参数列表传递给Python函数_Python_List_Function_Parameters_Tuples - Fatal编程技术网

将参数列表传递给Python函数

将参数列表传递给Python函数,python,list,function,parameters,tuples,Python,List,Function,Parameters,Tuples,如何将未知数量的参数传递到函数中?我用以下方式定义了一个函数 def func(x, *p): return ... 我试图传入一个值列表,用作参数。我尝试使用列表和元组,但函数总是返回零。有人有什么建议吗? 谢谢将值作为逗号分隔的值传递 >>> def func(x, *p): # p is stored as tuple ... print "x =",x ... for i in p: ... print i ...

如何将未知数量的参数传递到函数中?我用以下方式定义了一个函数

def func(x, *p):
return ...
我试图传入一个值列表,用作参数。我尝试使用列表和元组,但函数总是返回零。有人有什么建议吗?
谢谢

将值作为逗号分隔的值传递

>>> def func(x, *p):           # p is stored as tuple
...     print "x =",x
...     for i in p:
...         print i
...     return p
... 
>>> print func(1,2,3,4)        # x value 1, p takes the rest
x = 1
2
3
4
(2,3,4)                        # returns p as a tuple
你可以通过阅读

这相当于:

func("some", "values", "in", "a", "list")
func(5, "some", "values", "in", "a", "list")
固定的
x
参数可能需要考虑:

func(5, *some_list)
。。。相当于:

func("some", "values", "in", "a", "list")
func(5, "some", "values", "in", "a", "list")
如果没有为
x
指定值(
5
在上面的示例中),则
某些列表的第一个值将作为
x
参数传递给
func