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

查看:390
本文介绍了“if 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所说,你可以申请De Morgan定律并获得等价物:

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.

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

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