如何将多值真值表转换为条件判断/表达式并生成Python函数
多值真值表转高效Python函数的实现方案
问题背景
有如下包含所有输入组合的多值真值表,输入字段(Location、Weather、Temperature、Time of Day)均有固定取值范围,需要实现一个高效无冗余的Python函数,输入指定条件后返回对应的Activity:
| Location | Weather | Temperature | Time of Day | Activity |
|---|---|---|---|---|
| Indoors | Sunny | Hot | Morning | Reading |
| Indoors | Sunny | Hot | Evening | Watching TV |
| Indoors | Sunny | Cool | Morning | Reading |
| Indoors | Sunny | Cool | Evening | Watching TV |
| Indoors | Rainy | Hot | Morning | Reading |
| Indoors | Rainy | Hot | Evening | Watching TV |
| Indoors | Rainy | Cool | Morning | Reading |
| Indoors | Rainy | Cool | Evening | Watching TV |
| Outdoors | Sunny | Hot | Morning | Gardening |
| Outdoors | Sunny | Hot | Evening | Barbecue |
| Outdoors | Sunny | Cool | Morning | Playing Sports |
| Outdoors | Sunny | Cool | Evening | Barbecue |
| Outdoors | Rainy | Hot | Morning | Shopping |
| Outdoors | Rainy | Hot | Evening | Barbecue |
| Outdoors | Rainy | Cool | Morning | Shopping |
| Outdoors | Rainy | Cool | Evening | Barbecue |
| None | Sunny | Hot | Morning | Reading |
| None | Sunny | Hot | Evening | Barbecue |
| None | Sunny | Cool | Morning | Reading |
| None | Sunny | Cool | Evening | Shopping |
| None | Rainy | Hot | Morning | Reading |
| None | Rainy | Hot | Evening | Barbecue |
| None | Rainy | Cool | Morning | Shopping |
| None | Rainy | Cool | Evening | Shopping |
实现方案
方案1:字典映射(最简便高效)
利用Python字典的O(1)查询特性,将输入的组合元组作为键,对应的Activity作为值存入字典。这种方式实现简单,查询速度快,完全无冗余,适合输入组合固定的场景。
# 预定义所有输入组合与Activity的映射 activity_map = { ("Indoors", "Sunny", "Hot", "Morning"): "Reading", ("Indoors", "Sunny", "Hot", "Evening"): "Watching TV", ("Indoors", "Sunny", "Cool", "Morning"): "Reading", ("Indoors", "Sunny", "Cool", "Evening"): "Watching TV", ("Indoors", "Rainy", "Hot", "Morning"): "Reading", ("Indoors", "Rainy", "Hot", "Evening"): "Watching TV", ("Indoors", "Rainy", "Cool", "Morning"): "Reading", ("Indoors", "Rainy", "Cool", "Evening"): "Watching TV", ("Outdoors", "Sunny", "Hot", "Morning"): "Gardening", ("Outdoors", "Sunny", "Hot", "Evening"): "Barbecue", ("Outdoors", "Sunny", "Cool", "Morning"): "Playing Sports", ("Outdoors", "Sunny", "Cool", "Evening"): "Barbecue", ("Outdoors", "Rainy", "Hot", "Morning"): "Shopping", ("Outdoors", "Rainy", "Hot", "Evening"): "Barbecue", ("Outdoors", "Rainy", "Cool", "Morning"): "Shopping", ("Outdoors", "Rainy", "Cool", "Evening"): "Barbecue", ("None", "Sunny", "Hot", "Morning"): "Reading", ("None", "Sunny", "Hot", "Evening"): "Barbecue", ("None", "Sunny", "Cool", "Morning"): "Reading", ("None", "Sunny", "Cool", "Evening"): "Shopping", ("None", "Rainy", "Hot", "Morning"): "Reading", ("None", "Rainy", "Hot", "Evening"): "Barbecue", ("None", "Rainy", "Cool", "Morning"): "Shopping", ("None", "Rainy", "Cool", "Evening"): "Shopping", } def get_activity(location, weather, temperature, time_of_day): # 输入合法性校验,避免非法参数 valid_locations = {"Indoors", "Outdoors", "None"} valid_weathers = {"Sunny", "Rainy"} valid_temperatures = {"Hot", "Cool"} valid_times = {"Morning", "Evening"} if location not in valid_locations: raise ValueError(f"无效的Location:{location},可选值为{valid_locations}") if weather not in valid_weathers: raise ValueError(f"无效的Weather:{weather},可选值为{valid_weathers}") if temperature not in valid_temperatures: raise ValueError(f"无效的Temperature:{temperature},可选值为{valid_temperatures}") if time_of_day not in valid_times: raise ValueError(f"无效的Time of Day:{time_of_day},可选值为{valid_times}") return activity_map[(location, weather, temperature, time_of_day)]
方案2:规则提炼(精简代码)
先分析真值表提炼重复逻辑,再基于规则编写函数,代码更简洁易维护,适合需要后续修改规则的场景。从表中可提炼出以下规则:
- Location为Indoors时,Activity仅由Time of Day决定:Morning是Reading,Evening是Watching TV,与其他输入无关
- Location为Outdoors时,Evening的Activity固定为Barbecue;Morning时,Sunny+Hot对应Gardening,Sunny+Cool对应Playing Sports,Rainy不管温度都是Shopping
- Location为None时,Morning的Activity由Weather决定:Sunny对应Reading,Rainy对应Shopping;Evening的Activity由Temperature决定:Hot对应Barbecue,Cool对应Shopping
def get_activity(location, weather, temperature, time_of_day): # 输入合法性校验 valid_locations = {"Indoors", "Outdoors", "None"} valid_weathers = {"Sunny", "Rainy"} valid_temperatures = {"Hot", "Cool"} valid_times = {"Morning", "Evening"} if location not in valid_locations: raise ValueError(f"无效的Location:{location},可选值为{valid_locations}") if weather not in valid_weathers: raise ValueError(f"无效的Weather:{weather},可选值为{valid_weathers}") if temperature not in valid_temperatures: raise ValueError(f"无效的Temperature:{temperature},可选值为{valid_temperatures}") if time_of_day not in valid_times: raise ValueError(f"无效的Time of Day:{time_of_day},可选值为{valid_times}") if location == "Indoors": return "Reading" if time_of_day == "Morning" else "Watching TV" elif location == "Outdoors": if time_of_day == "Evening": return "Barbecue" else: if weather == "Sunny": return "Gardening" if temperature == "Hot" else "Playing Sports" else: return "Shopping" elif location == "None": if time_of_day == "Morning": return "Reading" if weather == "Sunny" else "Shopping" else: return "Barbecue" if temperature == "Hot" else "Shopping"
方案对比
- 字典映射:实现成本最低,查询速度最快,适合输入组合固定、无需频繁修改规则的场景。如果输入组合数量大,可以通过脚本读取CSV/表格文件自动生成字典,避免手动编写。
- 规则提炼:代码更精简,可读性和可维护性更强,适合能提炼出明确逻辑的场景,后续修改规则只需调整对应分支即可。
内容的提问来源于stack exchange,提问作者Saif
相关产品推荐
相关产品推荐

