Using a dictionary to select function to execute

前端 未结 10 2123
走了就别回头了
走了就别回头了 2020-11-27 13:05

I am trying to use functional programming to create a dictionary containing a key and a function to execute:

myDict={}
myItems=(\"P1\",\"P2\",\"P3\",....\"         


        
10条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-27 14:01

    You can just use

    myDict = {
        "P1": (lambda x: function1()),
        "P2": (lambda x: function2()),
        ...,
        "Pn": (lambda x: functionn())}
    myItems = ["P1", "P2", ..., "Pn"]
    
    for item in myItems:
        myDict[item]()
    

提交回复
热议问题