pyDay17
1、用filter求素数。
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
def _odd_iter():
n = 1
while True:
n = n + 2
yield n
# 这是一个从3开始的无限奇数序列 def _not_divisible(n):
return lambda x: x % n > 0
# 动态的筛选函数,如果不能被n整除就返回True def primes():
yield 2
it = _odd_iter()
while True:
n = next(it)
yield n
it = filter(_not_divisible(n), it) #不是n的倍数的留下! for n in primes():
if n < 1000:
print(n)
else:
break
D:\labs>python test.py
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
101
103
107
109
113
127
131
137
139
149
151
157
163
167
173
179
181
191
193
197
199
211
223
227
229
233
239
241
251
257
263
269
271
277
281
283
293
307
311
313
317
331
337
347
349
353
359
367
373
379
383
389
397
401
409
419
421
431
433
439
443
449
457
461
463
467
479
487
491
499
503
509
521
523
541
547
557
563
569
571
577
587
593
599
601
607
613
617
619
631
641
643
647
653
659
661
673
677
683
691
701
709
719
727
733
739
743
751
757
761
769
773
787
797
809
811
821
823
827
829
839
853
857
859
863
877
881
883
887
907
911
919
929
937
941
947
953
967
971
977
983
991
997
2、python中的lambda函数
lambda x:x%n>0
lambda语句中,冒号前的是参数,可以有多个,用逗号隔开,冒号右边的是返回值。
pyDay17的更多相关文章
随机推荐
- discuz x 系列目录结构说明
api ┄┄┄外部接口 connect ┄┄┄腾讯互联 db ┄┄┄UCenter数据库备份接口 google ┄┄┄Google引擎使用 javascript ┄┄┄数据和广告的 J ...
- [置顶] 数据库优化实践【MS SQL优化开篇】
数据库定义: 数据库是依照某种数据模型组织起来并存在二级存储器中的数据集合,此集合具有尽可能不重复,以最优方式为特定组织提供多种应用服务,其数据结构独立于应用程序,对数据的CRUD操作进行统一管理和控 ...
- 【Spring Boot && Spring Cloud系列】构建Springboot项目 实现restful风格接口
项目代码如下: package hello; import org.springframework.boot.SpringApplication; import org.springframework ...
- 索引原理 B tree
数据库原理之-索引 背景介绍: 用数据库的时候经常有几个疑问: 1:为啥通过加索引就能提升数据的查询料率? 2:为啥加多了索引会导致增删改的效率变低? 3:为啥有的人能用好有的人用不好? 这些问题我们 ...
- 解决Chrome关联Html文件图标显示为空白
用记事本保存为ChromeHTML.reg Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\CLSID\{42042206-2D85-1 ...
- 【BZOJ3037/2068】创世纪/[Poi2004]SZP 树形DP
[BZOJ3037]创世纪 Description applepi手里有一本书<创世纪>,里面记录了这样一个故事……上帝手中有着N 种被称作“世界元素”的东西,现在他要把它们中的一部分投放 ...
- 把 Activity 改成 ListActivity继续使用 setContentView
ListActivity has a default layout that consists of a single, full-screen list in the center of the s ...
- java类的成员变量和局部变量的区别
转自:https://jingyan.baidu.com/article/03b2f78c1ba2d05ea237ae9b.html 在类中位置不同:成员变量:在类中方法外.局部变量:在方法定义中或者 ...
- 几种常用的SQL优化工具及方法
转自:http://blog.itpub.net/35489/viewspace-764856/ 1. sql 详细执行计划,主要检查驱动路径,索引是否合适:同一个pl/sql窗口连续执行即可:exp ...
- (转)梯度下降法及其Python实现
梯度下降法(gradient descent),又名最速下降法(steepest descent)是求解无约束最优化问题最常用的方法,它是一种迭代方法,每一步主要的操作是求解目标函数的梯度向量,将当前 ...