"if a or b or c but not all of them"的 Python 语法

标签 python if-statement

我有一个可以接收零个或三个命令行参数的 python 脚本。 (要么以默认行为运行,要么需要指定所有三个值。)

以下内容的理想语法是什么:

if a and (not b or not c) or b and (not a or not c) or c and (not b or not a):

?

最佳答案

如果你的意思是一个最小的形式,那就去吧:

if (not a or not b or not c) and (a or b or c):

这会翻译您的问题的标题。

更新:正如 Volatility 和 Supr 正确所说,您可以应用德摩根定律并获得等价物:

if (a or b or c) and not (a and b and c):

我的建议是使用对您和其他程序员更重要的形式。第一个意思是“有些东西是假的,但也是真的”,第二个意思是“有些东西是真的,但不是全部”。如果我要在硬件中进行优化或执行此操作,我会选择第二个,这里只选择最易读的(同时考虑您将要测试的条件及其名称)。我选了第一个。

关于 "if a or b or c but not all of them"的 Python 语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16522111/

相关文章:

Python 猴子补丁

python - 如何从继承字符串类型的新类访问字符串值

python - 通过 git grep 查找同时包含两个指定术语的文件

python - 快速精确的 Python 重复计时器

javascript - 如何根据下拉选择显示/隐藏 div?

batch-file - 为什么 IF 检查在检查表达式之后会忽略 "^<delimter>some_word"?

R语言,统计一个月中的星期五

java - 如何查找数组中整数的长度

python - scikit KernelPCA 结果不稳定

excel - 如何跨从过滤数据中提取的多个工作表编写 countifs 函数?