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()把传入的函数依次作用于每个元素,然后根据返回值是 ...
随机推荐
- Spring-data-JPA详细介绍
Spring-data-JPA学习: 1. https://blog.csdn.net/liujianwd/article/details/75411009 2.http://www.cnblogs. ...
- DevExpress WinForms使用教程:新的CheckEdit样式
[DevExpress WinForms v18.2下载] 在最开始CheckEdit控件有16种样式, 使用CheckStyle属性,开发人员可以选择其中一种样式.随着时间推移,与其他Windows ...
- L319 Zigbee test coding- field test fail-base on EFR32MG1
1 Test coding Zigbee test of Tx power and frequency for every channel. Testing Procedure1) Power up ...
- day 71-72 cookie 和session
拓展知识 request---->请求信息 属性: request.path # 获取访问文件路径 request.method属性 #获取请求中使用的HTTP方式(POST/G ...
- python之pandas用法大全
python之pandas用法大全 更新时间:2018年03月13日 15:02:28 投稿:wdc 我要评论 本文讲解了python的pandas基本用法,大家可以参考下 一.生成数据表1.首先导入 ...
- jQuery .each()方法与.data()方法
.each(callback): 每次执行传递进来的函数时,函数中的this关键字都指向一个不同的DOM元素(每次都是一个不同的匹配元素).而且,在每次执行函数时,都会给函数传递一个表示作为执行环境的 ...
- shell脚本-预定义常量
$0 这个程式的执行名字$n 这个程式的第n个参数值,n=1..9$* 这个程式的所有参数,此选项参数可超过9个.$# 这个程式的参数个数$$ 这个程式的PID(脚本运行的当前进程ID号)$! 执行上 ...
- 基于C#利用ffmpeg提取视频帧
利用ffmepg提取视频帧实际上是利用C#调用ffmepg命令行进行处理对应的视频,然后输出出视频帧 GetPicFromVideo("); static public string Get ...
- Codeforces 978E:Bus Video System
题目链接:http://codeforces.com/problemset/problem/978/E 题意 一辆公交车,在每站会上一些人或下一些人,车的最大容量为w,问初始车上可能有的乘客的情况数. ...
- 用户密码管理和 su 命令
1.passwd root 用户给自己改 密码,直接 输入 passwd 就可以了 若是给其它用户修改密码,就需要 passwd user_name 用户锁定和解锁 passwd -l user_ ...