【问题标题】:how to pass parameter in a function which is a value of dictionary in python如何在作为python中字典值的函数中传递参数
【发布时间】:2022-11-02 18:57:15
【问题描述】:

我有两本字典

dict1={
"abc":sm.xyz.cdd(),
"def":sm.acf.fdr(),
"ghi":sm.rty.qsd(),
}


我需要创建一个带有 2 个参数的函数,第一个是 dict1 的键,第二个参数是一个字符串,我想要输出

fun("abc","log")

#then this must be my output
sm.xyz.cdd("log")

【问题讨论】:

  • 这是一本字典……你的 dict1 应该是dict1={"abc":sm.xyz.cdd, ...}(没有括号),否则你已经调用了一个函数。至于功能,到目前为止,您尝试过什么来实现它?

标签: python python-3.x dictionary


【解决方案1】:
dict1={
  "abc":sm.xyz.cdd,
  "def":sm.acf.fdr,
  "ghi":sm.rty.qsd,
}
def fun(key, **vaargs):
    dict1[key](**vaargs)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-26
    • 2022-11-11
    • 1970-01-01
    • 2019-05-19
    • 2021-09-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多