-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfilter.py
More file actions
47 lines (32 loc) · 796 Bytes
/
filter.py
File metadata and controls
47 lines (32 loc) · 796 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# !/usr/bin/env python
# -*- coding:utf-8 -*-
# filter过滤
def no_empty(s):
return s and s.strip()
# no_empty 函数返回true 则输出输入的字符, false不返回输入的字符
r_no = filter(no_empty, ['A', '', 'B', None, 'C', ' '])
print(list(r_no))
# 使用filter 赛选素数
# 生成素数
def generate_shu():
n = 1
while True:
n = n + 2
yield n
# 筛选规则
def filter_rule(n):
return lambda x: x % n == 0;
# 取出数据,进行筛选
def filter_data():
yield 2 # 表示先返回一个2 但函数还会继续执行下去
it = generate_shu()
while True:
n = next(it)
yield n
it = filter(filter_rule, it)
# 测试
for n in filter_data():
if n <= 1000:
print(n)
else:
break