fal*_*cin 42 python list argument-unpacking
从列表中提取项目并将它们作为参数传递给函数调用的简单方法是什么,如下例所示?
例:
def add(a,b,c,d,e):
print(a,b,c,d,e)
x=(1,2,3,4,5)
add(magic_function(x))
Run Code Online (Sandbox Code Playgroud)
Cat*_*lus 65
您可以使用星形将元组或列表解压缩到位置参数中.
def add(a, b, c):
print(a, b, c)
x = (1, 2, 3)
add(*x)
Run Code Online (Sandbox Code Playgroud)
同样,您可以使用double star将dict解压缩为关键字参数.
x = { 'a': 3, 'b': 1, 'c': 2 }
add(**x)
Run Code Online (Sandbox Code Playgroud)
Tim*_*ker 11
我认为你的意思是*
拆包运营商:
>>> l = [1,2,3,4,5]
>>> def add(a,b,c,d,e):
... print(a,b,c,d,e)
...
>>> add(*l)
1 2 3 4 5
Run Code Online (Sandbox Code Playgroud)