如何使用字典在python中编写开关大小写
我在python代码中使用嵌套if,示例代码如下:如何使用字典在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()
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']
看起来像什么?可能是重复的