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()把传入的函数依次作用于每个元素,然后根据返回值是 ...
随机推荐
- PM2报错‘Spawning PM2 daemon with pm2_home...’的解决方案
问题 在某次因为SRE升级域名问题,导致了Node服务器代码死循环了,产生的504(Gateway timeout)错误. 登录到机器上看,正在用pm2查问题的原因中,突然发现错误从504变成的502 ...
- VS2017企业版的密钥
Visual Studio 2017(VS2017) 企业版 Enterprise 注册码:NJVYC-BMHX2-G77MM-4XJMR-6Q8QFVisual Studio 2017(VS2017 ...
- L257 论述型作文模板
一引出相同观点: 1.With the development of science and technology, more and more people believe that...随着科技的 ...
- http 协议三次握手
HTTP是超文本传输协议,信息是明文传输.TPC/IP协议是传输层协议,主要解决数据如何在网络中传输.HTTP是应用层协议,主要解决如何包装数据. [HTTP与TCP/IP]和其他的协议在最初OSI模 ...
- kbmMW 5.07.00试用笔记
在kbmMW 5.06.20试用笔记中遇到的问题,在这个版本中,基本都解决了.但还是发现修正后存在的小问题及新问题: 1.Resolve返回值错误 当提交的ClientQuery是执行一条sql语句, ...
- 【Python】多线程-2
1. 进程和线程的区别: (1) 一个进程可以有多个线程,一个进程中的多个线程共享该进程的所有资源,多线程切换比多进程切换快,因为不用上下文切换,Python中并发建议用多进程 (2) 进程是资 ...
- SQL注入之Sqli-labs系列第十二关
开始挑战第十二关(Error Based- Double quotes- String) 12点半了,不困,继续,继续,继续 先看看页面,通常的使用单引号等进行操作,看看啥么情况先 咦,出现错误信息了 ...
- C# process 隐藏应用程序的进度条
命令行参数那加上-ibck指定后台运行. string sourceFilepath = "d:\\测试.rar"; string targetFilepath = "d ...
- Python之路PythonNet,第三篇,网络3
pythonnet 网络3 udp 通信 recvfrom sendtofork 多进程并发threading 多线程并发socketserver 系统模块 套接字的属性 setsockopt g ...
- SyntaxError: Non-UTF-8 code starting with '\xe5' in file ***.py on line 105, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for
用charles抓包时, 对抓到的html,放到pycharm中解析, 结果报错: SyntaxError: Non-UTF-8 code starting with '\xe5' in file * ...