python, 用filter实现素数
# _*_ coding:utf-8 _*_
#step1: 生成一个序列
def _odd_iter():
n = 1
while True:
n = n + 1
yield n
#Step2: 定义筛选函数
def _not_divisible(n):
return lambda x: x % n >0
#step3:定义生成器,不断的返回下一个素数
def primes():
it = _odd_iter()#初始序列
while True:
n = next(it)
yield n
it = filter(_not_divisible(n), it)
#test
for n in primes():
if n < 20:
print(n)
else:
break
python, 用filter实现素数的更多相关文章
- python中用filter求素数
#用filter求素数 #生成器,生成一个无限序列 def _odd_iter(): n=1 while True: n=n+2 yield n #筛选函数 def _not_divisible(n) ...
- python基础——filter函数
python基础——filter函数 Python内建的filter()函数用于过滤序列. 和map()类似,filter()也接收一个函数和一个序列.和map()不同的是,filter()把传入的函 ...
- python 内建函数 filter,map和reduce
python 内建函数 filter,map和reduce, 三个函数比较类似,都是应用于序列的内置函数,常见的序列包括list.tuple.str等.而且三个函数都可以和lambda表达式结合使用. ...
- python中filter、map、reduce的区别
python中有一些非常有趣的函数,今天也来总结一下,不过该类的网上资料也相当多,也没多少干货,只是习惯性将一些容易遗忘的功能进行整理. lambda 为关键字.filter,map,reduce为内 ...
- Python map,filter,reduce函数
# -*- coding:utf-8 -*- #定义一个自己的map函数list_list = [1,2,4,8,16] def my_map(func,iterable): my_list = [] ...
- python求100以内素数
python求100以内素数之和 from math import sqrt # 使用isPrime函数 def isPrime(n): if n <= 1: return False for ...
- Python之filter筛选数据工具
# -*- coding: utf-8 -*- #python 27 #xiaodeng #Python之filter筛选数据工具 #http://python.jobbole.com/82597/ ...
- python中filter函数
python中filter()函数 filter()函数是 Python 内置的另一个有用的高阶函数,filter()函数接收一个函数 f 和一个list,这个函数 f 的作用是对每个元素进行判断 ...
- python之filter过滤器
Python内建的filter()函数用于过滤序列. 和map()类似,filter()也接收一个函数和一个序列.和map()不同的时,filter()把传入的函数依次作用于每个元素,然后根据返回值是 ...
随机推荐
- L275 Climate Change Is Having a Major Impact on Global Health
Climate Change Is Having a Major Impact on Global Health A devastating heat wave swept across Europe ...
- Nginx web服务器
文件读取会使用到以下几个配置 1. sendfile 使用nginx作为静态资源服务时,通过配置sendfile可以有效提高文件读取效率,设置为on表示启动高效传输文件的模式.sendfile可以让N ...
- 适用于WIFI Hacking的无线网卡推荐
相信很多初次使用Kali Linux来进行无线渗透的小伙伴都曾遇到过一个非常头疼的问题,就是不知道如何选购一款合适的无线网卡.因为Kali Linux并不是所有的网卡都支持,一旦选错了网卡不仅会给我们 ...
- mysql创建用户并给用户分配权限
1.登录Mysql [root@xufeng Desktop]# mysql -u root -pEnter password: Welcome to the MySQL monitor. Comma ...
- SpringBoot2静态资料访问
在SpringBoot2内要继承WebMvcConfigurationSupport并重写addResourceHandlers方法才能访问到静态资料. @Configuration public c ...
- Spring Boot 揭秘与实战(四) 配置文件篇 - 有哪些很棒的特性
文章目录 1. 使用属性文件2. YAML文件 1.1. 自定义属性 1.2. 参数引用 1.3. 随机数属性 1.4. application-{profile}.properties参数加载 3. ...
- Ajax的简单介绍与使用
1.什么是Ajax? Ajax(Asynchronous JavaScript and XML),简单说就是不需要刷新当前页面而实现javaScript和和后台服务器交换数据以更新网页中的部分内容. ...
- 【转载】 看996ICU
原文地址: https://www.jianshu.com/p/15d8726fa8a8 作者:Demisstif 来源:简书 ------------------------------------ ...
- php在foreach中使用引用赋值&可能遇到的问题(转)
楼主在写项目的时候,由于初涉PHP的赋值引用操作,觉得这个功能非常强大,用时一时爽,没有深入了解过其中的原理,导致了一些当时觉得不可思议的BUG,废话不都说,我举个例子详细的描述一下这个问题. 代码: ...
- CodeForces - 1101G :(Zero XOR Subset)-less(线性基)
You are given an array a1,a2,…,an of integer numbers. Your task is to divide the array into the maxi ...