我想使用一组 boolean 值作为字典的键,所以 (weather == sunny and temp == warm) 将是 11 or True,True while (weather == sunny and weather == cold) 将是10 and (weather == cloudy and weather == cold) True,False 将是 00 where clothing = {11:"shorts", 10:"jeans", 00:"jacket"} 有没有办法做到这一点?我假设它可能需要进行位操作,并且我正在努力使操作时间尽可能快。
最佳答案
如果您实际上不需要对各个条件进行按位运算(即,您不需要同时对两个条件进行 AND/OR),那么使用 boolean 元组作为键可能会更简单:
clothing = {
(True, True): "shorts",
(True, False): "jeans",
(False, False): "jacket"
}
关于字典的 python boolean 键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16256818/