字典的 python boolean 键

标签 python dictionary boolean boolean-logic

我想使用一组 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/

相关文章:

javascript - 使用 ACE 语法高亮显示 Flask-Admin 字段覆盖

python - 如何获取传递给堆栈上函数的参数值?

具有互斥可选参数的 python 类/子类,使用父类(super class)的实例作为可能的构造函数

python - 使用列表中的值过滤字典中的键、值

python - 为字典预分配内存?

Python Argparse 有条件地需要参数

java - 将映射键与字符串列表进行比较

c# - 文化不变 bool.TryParse

ios - Objective-C中的bool、Boolean、BOOL有区别吗?

excel - 我应该在公式中使用什么 boolean 值,true, "true"或 true()