您好,欢迎来到星星旅游。
搜索
您的当前位置:首页python3利用列表生成式、lambda函数快速返回素数列表

python3利用列表生成式、lambda函数快速返回素数列表

来源:星星旅游

原题:

Exercise 2: Prime challenge
Write the function primes(n) that return a list with all prime numbers up to n
Use lambda functions and list comprehensions.
Try your best to complete it with as little code as possible

 

def primes(n,end):
    unprimelist_func = lambda x: [y for y in range(2, x) if x % y == 0] #
    primelist = [x for x in range(n,end) if not unprimelist_func(x)] #不知道为什么改成 if x not in ...就会无效

    return primelist

def main():
    n=10   #开始位置
    end=30 #结束位置
    aimlist=primes(n,end)
    print(aimlist)



if __name__ == '__main__':
    main()

 

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- stra.cn 版权所有 赣ICP备2024042791号-4

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务