参考: python--参数列表的分拆
当你要传递的参数已经是一个列表,调用的函数却接受分开一个个的参数,这个时候可以考虑参数列表拆分:
- 可以使用* 操作符来自动把参数列表拆开:
args=[3,6] x=list(range(*args)) print(x) -------------------输出----------------------------- [3, 4, 5]
- 可以使用 ** 操作符分拆关键字参数为字典:
def parrot(voltage,state='a stiff',action='voom'):
print("-- This parrot wouldn't ",action)
print("if you put ",voltage,"volts through it.")
print("E's",state,"!")
d={"voltage":"four million", "state":"bleedin demised","action":"voom"}
parrot(**d)
-------------------输出-----------------------------
-- This parrot wouldn't voom if you put four million volts through it. E's bleedin demised !