Uh oh!
There was an error while loading. Please reload this page.
forked from SergioJune/python_test
- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest9.py
More file actions
Latest commit
50 lines (38 loc) · 1.13 KB
/
Copy pathtest9.py
File metadata and controls
50 lines (38 loc) · 1.13 KB
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
48
49
50
# 练习函数式编程的filter过滤器
fromfunctoolsimportreduce
L= [xforxinrange(100)]
# 过滤取3的倍数
L=filter(lambdax: x%3==0, L) # 这个返回也是一个迭代器,可以通过next来获取下一个元素
print(next(L))
# 转成list
print(list(L))
# 获取初始序列,因为偶数都不是素数,所以就只有奇数
defget_list():
n=1
whileTrue:
n=n+2
yieldn
# 获取素数
defget_primes():
num=2
yieldnum
# 初始化序列
it=get_list()
whileTrue:
num=next(it)
yieldnum
# 排除第一个数的倍数
filter(lambdax: x%num!=0, it)
fornuminget_primes():
ifnum<1000:
print(num)
else:
break
# 作业:回数是指从左向右读和从右向左读都是一样的数,例如12321,909。请利用filter()筛选出回数
defis_palindrome(n):
l=list(str(n))
l.reverse()
num=reduce(lambdax, y: int(x) *10+int(y), l)
returnnum==n
output=filter(is_palindrome, range(1, 1000))
print('1~1000:', list(output))