简体   繁体   English

列表理解功能可从一系列数字中查找所有素数?

[英]List comprehension for finding all prime numbers from a range of numbers?

[x for x in range(1,100) if all(x%y for y in range(2,x))]

Not able to understand how above code is returning correct results, Instead it should be 无法理解上面的代码如何返回正确的结果,相反应该

[x for x in range(1,100) if all(x%y!=0 for y in range(2,x))]

The reason is if all is True if only one of the condition is True . 原因是如果仅条件之一为True则全部为True

Non-zero integers are considered truthy in a boolean context, so the code snippets are equivalent. 在布尔上下文中,非零整数被认为是真实的,因此代码段是等效的。

Note that this algorithm incorrectly classifies 1 as a prime number. 请注意,此算法错误地将1分类为质数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM