Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用字典在python中编写开关大小写_Python - Fatal编程技术网

如何使用字典在python中编写开关大小写

如何使用字典在python中编写开关大小写,python,Python,我在python代码中使用嵌套if,示例代码如下: message = event_data["event"] if message.get("subtype") is None : if "friday" in message.get('text'): callfridayfunction() if "saturday" in message.get('text'): callsaturdayfunction()

我在python代码中使用嵌套if,示例代码如下:

message = event_data["event"]
    if message.get("subtype") is None :

       if "friday" in message.get('text'):
          callfridayfunction()

       if "saturday" in message.get('text'):
          callsaturdayfunction()

       if "monday" in message.get('text'):
          callmondayfunction()

如何将其作为开关盒或使用字典编写?请帮助

这可能是最接近您想要的。如果文本中也有多天,这将起作用

days_switch = {
    'monday': callmondayfunction,
    'saturday': callsaturdayfunction
    'friday': callfridayfunction
}

message = event_data["event"]
if message.get("subtype") is None :
    text = message.get('text', '')
    days = set(text.split()) & set(days_switch)
    for day in days:
        days_switch[day]()
如果您知道文本中没有多天,则不需要循环

days_switch[set(text.split()) & set(days_switch)]()

这可能是最接近你想要的。如果文本中也有多天,这将起作用

days_switch = {
    'monday': callmondayfunction,
    'saturday': callsaturdayfunction
    'friday': callfridayfunction
}

message = event_data["event"]
if message.get("subtype") is None :
    text = message.get('text', '')
    days = set(text.split()) & set(days_switch)
    for day in days:
        days_switch[day]()
如果您知道文本中没有多天,则不需要循环

days_switch[set(text.split()) & set(days_switch)]()

例如,
message['text']
看起来像什么?例如,
message['text']
看起来像什么?可能是重复的