我想使用一组布尔值作为字典的关键字,这样(weather == sunny == temp == want )将是11或True,True而(weather == sunny == weather == clothing )将是10,(weather == cloudy == weather ==clothing) True,False将是00 where where= {11:"shorts",10:"jeans",00:“夹克”}有什么方法可以做到吗?我假设它可能需要位操作,我正在尝试尽可能快地保持这一点,以获得操作时间。
发布于 2013-04-28 05:05:33
如果您实际上不需要对单个条件执行逐位操作(即,您不需要将两个条件一起进行AND/OR操作),那么使用布尔值元组作为键可能会更简单:
clothing = {
    (True, True): "shorts",
    (True, False): "jeans",
    (False, False): "jacket"
}发布于 2013-04-28 05:03:20
您可以使用按位或(|)运算符:
sunny = 0
cloudy = 1
cold = 2
clothing = { (cold|cloudy) :"shorts", cold:"jeans", sunny:"jacket"}
weather = something()
print(clothing[ weather & (cold|cloudy) ])但是@BrenBarn推荐的元组版本更好。
https://stackoverflow.com/questions/16256818
复制相似问题