我在程序中有一行代码,用于检查数字是否可以被多个数字整除。
然而,我目前的方式是非常低效和丑陋的。
它目前看起来像:
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'更多推荐
发布评论