“如果 a 或 b 或 c 但不是全部"的 Python 语法 [英] Python syntax for "if a or b or c but not all of them"

查看:16
本文介绍了“如果 a 或 b 或 c 但不是全部"的 Python 语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a python script that can receive either zero or three command line arguments. (Either it runs on default behavior or needs all three values specified.)

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

What's the ideal syntax for something like:

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 you mean a minimal form, go with this:

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

翻译问题的标题.

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

UPDATE: as correctly said by Volatility and Supr, you can apply De Morgan's law and obtain equivalent:

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

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

My advice is to use whichever form is more significant to you and to other programmers. The first means "there is something false, but also something true", the second "There is something true, but not everything". If I were to optimize or do this in hardware, I would choose the second, here just choose the most readable (also taking in consideration the conditions you will be testing and their names). I picked the first.

这篇关于“如果 a 或 b 或 c 但不是全部"的 Python 语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆