好吧,我真的不知道如何用语言来描述这一点,但我有一个简单的例子:
def foo(x,y,z):
r=x+....
a = [1,2,3]
foo(a[0], a[1], a[2])
我可以在不多次调用 a[n] 的情况下做同样的事情吗?但保持“x,y,z”定义?
请您参考如下方法:
使用 unpacking operator !
>>> def foo(x,y,z):
... return x+y+z
...
>>> a = [1,2,3]
>>> foo(*a)
6
来自文档
If they are not available separately, write the function call with the *-operator to unpack the arguments out of a list or tuple:
>>> args = [3, 6] >>> range(*args) # call with arguments unpacked from a list [3, 4, 5]
