python 用键传递字典参数
python passing dictionary parameter with key
我想用像这样的字典进行函数调用:
valueattime(simulaton_01['Temperature_05'], 20)
但我总是遇到以下函数的语法错误
def valueattime(experiment[key], time):
...
它使用简单的参数。但是为了 autocompletion
的缘故,以字典形式传递参数会很棒。
有什么想法吗?
无需更改函数签名即可直接从字典传递参数:
def valueattime(temperature, time):
...
valueattime(simulation_01['temp_05'], 20) # this works just fine
Python会先运行simulation_01['temp_05']
取值,然后作为temperature
传给函数
您应该将其作为常规参数传递:
def valueattime(temp, time):
pass
a = {'some_key': 'some_value'}
valueattime(a['some_key], 20)
这有效!
要为函数提供字典项,您可以使用字典理解:
new_dict = {k: valueattime(v) for k, v in a.iteritems()}
记住这是所有对象...
def valueattime(temp, time)
定义了一个方法接受两个输入参数,两个对象由 names temp
和 time
引用
simulation_01['temp_05']
return 一个对象,所以像这样调用你的方法:valueattime( simulation_01['temp_05'], 20 )
应该是你要找的东西
我想用像这样的字典进行函数调用:
valueattime(simulaton_01['Temperature_05'], 20)
但我总是遇到以下函数的语法错误
def valueattime(experiment[key], time):
...
它使用简单的参数。但是为了 autocompletion
的缘故,以字典形式传递参数会很棒。
有什么想法吗?
无需更改函数签名即可直接从字典传递参数:
def valueattime(temperature, time):
...
valueattime(simulation_01['temp_05'], 20) # this works just fine
Python会先运行simulation_01['temp_05']
取值,然后作为temperature
传给函数
您应该将其作为常规参数传递:
def valueattime(temp, time):
pass
a = {'some_key': 'some_value'}
valueattime(a['some_key], 20)
这有效!
要为函数提供字典项,您可以使用字典理解:
new_dict = {k: valueattime(v) for k, v in a.iteritems()}
记住这是所有对象...
def valueattime(temp, time)
定义了一个方法接受两个输入参数,两个对象由 names temp
和 time
simulation_01['temp_05']
return 一个对象,所以像这样调用你的方法:valueattime( simulation_01['temp_05'], 20 )
应该是你要找的东西