我有一个可以接收零个或三个命令行参数的 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/