我怎样才能压缩这个简单的“if”行代码?(How can I condense this simple “if” line of code?)

编程入门 行业动态 更新时间:2024-10-28 06:30:30
我怎样才能压缩这个简单的“if”行代码?(How can I condense this simple “if” line of code?)

我在程序中有一行代码,用于检查数字是否可以被多个数字整除。

然而,我目前的方式是非常低效和丑陋的。

它目前看起来像:

if(x % 1 == 0 and x % 2 == 0 and x % 3 == 0)等等。 我希望有人能教我如何取长补短,并帮助我用更有意义的东西取而代之,而不是像下面这样:

if(x % a[1,2,3] == 0)

这是可能的,如果有的话,任何人都可以帮助我吗?

编辑:x%1 == 0,x%2 == 0等等。不是完整的等式。 我正在检查远远超过%1,2和3.我正在寻找一个解决方案,可以说.. 1到15.这就是为什么我不使用x%6。

I have a line of code in my program that checks if a number is divisible by several numbers.

However, the way I currently have it is extremely inefficient and ugly.

It currently looks like:

if(x % 1 == 0 and x % 2 == 0 and x % 3 == 0) and so forth for several other numbers. I was hoping someone could teach me how I could take out the long and chain and help me replace it with something that makes more sense so instead it will look like:

if(x % a[1,2,3] == 0)

Is this possible and if so can anyone please help me?

EDIT: The x % 1 == 0, x % 2 == 0, etc.. Is not the full equation. I am checking far more than % 1,2, and 3. I am looking for a solution that can take say.. 1 through 15. That is why I am not using x % 6.

最满意答案

if all(x % k == 0 for k in [1, 2, 3]): print 'yay' if all(x % k == 0 for k in [1, 2, 3]): print 'yay'

更多推荐

本文发布于:2023-08-08 00:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466712.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:代码   简单   condense   code   line

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!