[英]Find prime numbers with list comprehension
I was trying to write a code to select the prime numbers in a list.我试图编写代码到 select 列表中的素数。 The user gives a limit and the program displays all prime number from 2 to the limit.
用户给出一个极限,程序显示从 2 到极限的所有素数。 I was trying to reduce the maximum amount of lines I could and was surprised with some situations I can't understand.
我试图尽可能减少行数,但对一些我无法理解的情况感到惊讶。 If you can help me, I'd be grateful.
如果你能帮助我,我将不胜感激。
I wrote this code:我写了这段代码:
# returns all integers from 2 to a limit given by the user.
def primes(limit):
# generates the numbers.
lista = range(2, limit + 1)
p = 2
while p < limit:
#filters the prime numbers and places in a list.
lista = [i for i in lista if i == p or i % p != 0]
p += 1
return lista
def main():
#asks the user for the limit number.
l = int(input("Enter the limit: "))
#call the function which selects the numbers and returns the result.
return print(primes(l))
#Ensures that the main program only runs when the functions have not been imported into another file.
if __name__ == '__main__':
main()
It runs as I expected, but when I tried deleting the first list assignment line and include the range function directly into the comprehension, it doesn't work.它按我的预期运行,但是当我尝试删除第一个列表分配行并将范围 function 直接包含到理解中时,它不起作用。 Why?
为什么?
# returns all integers from 2 to a limit given by the user.
def primes(limit):
p = 2
while p < limit:
#filters the prime numbers and places in a list.
lista = [i for i in range(2, limit + 1) if i == p or i % p != 0]
#or lista = [i for i in range(2, limit + 1) if i == p or i % p != 0]
#or lista = [i for i in [*range(2, limit + 1)] if i == p or i % p != 0]
p += 1
return lista
def main():
#asks the user for the limit number.
l = int(input("Enter the limit: "))
#call the function which selects the numbers and returns the result.
return print(primes(l))
#Ensures that the main program only runs when the functions have not been imported into another file.
if __name__ == '__main__':
main()
Other problem.其他问题。 As the line with range is not a list I fixed it only to improve the code, but when I changed the name of the value from 'lista' to another name, I saw that it doesn't work too.
由于范围行不是列表,我修复它只是为了改进代码,但是当我将值的名称从“lista”更改为另一个名称时,我发现它也不起作用。 Why?
为什么?
# returns all integers from 2 to a limit given by the user.
def primes(limit):
# generates the numbers.
nums = range(2, limit + 1)
p = 2
while p < limit:
#filters the prime numbers and places in a list.
lista = [i for i in nums if i == p or i % p != 0]
p += 1
return lista
def main():
#asks the user for the limit number.
l = int(input("Enter the limit: "))
#call the function which selects the numbers and returns the result.
return print(primes(l))
#ensures that the main program only runs when the functions have not been imported into another file.
if __name__ == '__main__':
main()
Thanks for your attention.感谢您的关注。
This one-liner works perfectly:这个单线完美地工作:
def primes(val):
return [x for x in range(2, val) if all(x % y != 0 for y in range(2, x))]
print(primes(10))
Thank you for your attention.I liked the answer of our friend Yash Makan, but when I tried larger numbers, like 100000, it never led me to the result (or I was not so patient to wait).感谢您的关注。我喜欢我们的朋友 Yash Makan 的回答,但是当我尝试更大的数字时,比如 100000,它从来没有让我得到结果(或者我没有耐心等待)。 So I continued thinking about the problem and got the following that is the fastest way to compute this problem I could achieve with list comprehension.
所以我继续思考这个问题并得到以下是计算这个问题的最快方法,我可以通过列表理解实现。 Note how fast you can compute millions of numbers.
请注意计算数百万个数字的速度。
# returns all integers from 2 to a limit given by the user.
def primes(limit):
l = [i for i in range(2, limit + 1) if all(i % j != 0 for j in [2, 3, 5])]
lista = []
return [2, 3, 5] + [lista.append(i) or i for i in l if all( i % j != 0 for j in lista[:int((len(lista) ** .5) + 1)])]
def main():
l = int(input("Enter the limit: "))
return print(primes(l))
if __name__ == '__main__':
main()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.