Python学习札记(二十二) 函数式编程3 filter & SyntaxError: unexpected EOF while parsing
参考:
Problem
SyntaxError: unexpected EOF while parsing
遇到该语法错误,一般是由于 括号不匹配 问题。
Note
1.filter 用于过滤list,关键在于正确实现一个“筛选”函数。
eg.过滤得到偶数:
#!/usr/bin/env python3
L = []
# input 10 integers
for i in range(10) :
L.append(int(input()))
# define filter function => bool judge
def filter_func(x) :
return x%2 == 1
print(list(filter(filter_func, L)))
sh-3.2# ./filter1.py
10
9
8
7
6
5
4
3
2
1
[9, 7, 5, 3, 1]
filter function 是过滤判断函数,当返回为True时执行过滤。
2.filter()函数返回的是一个Iterator,也就是一个惰性序列,所以要强迫filter()完成计算结果,需要用list()函数获得所有结果并返回list。
3.埃式筛法:
#!/usr/bin/env python3
# define a filter function
def filter_func(x) :
return lambda i: i%x > 0
# Iterator A: Initial Array
def creater() :
n = 1
while True:
n = n+2
yield(n)
def E_primes() :
yield(2)
it = creater() # Initial array
while True:
n = next(it) # iterator => next()
yield(n)
it = filter(filter_func(n), it) # creat new array
def main() :
primes = []
for i in E_primes() :
if i <= 1000 :
primes.append(i)
else :
break
print(primes)
if __name__ == '__main__':
main()
[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]
练习
回数是指从左向右读和从右向左读都是一样的数,例如12321,909。请利用filter()滤掉非回数:
#!/usr/bin/env python3
def is_palindrome(n) :
i, j, cnt = n, n, 0
while i > 0 :
cnt = cnt+1
i = int(i/10) # hint
ans = True
s = str(j)
for i in range(0, cnt) :
if s[i] != s[cnt-1-i] :
ans = False
break
return ans
def main() :
output = filter(is_palindrome, range(1, 1000))
print(list(output))
if __name__ == '__main__':
main()
sh-3.2# ./filter3.py
[1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121, 131, 141, 151, 161, 171, 181, 191, 202, 212, 222, 232, 242, 252, 262, 272, 282, 292, 303, 313, 323, 333, 343, 353, 363, 373, 383, 393, 404, 414, 424, 434, 444, 454, 464, 474, 484, 494, 505, 515, 525, 535, 545, 555, 565, 575, 585, 595, 606, 616, 626, 636, 646, 656, 666, 676, 686, 696, 707, 717, 727, 737, 747, 757, 767, 777, 787, 797, 808, 818, 828, 838, 848, 858, 868, 878, 888, 898, 909, 919, 929, 939, 949, 959, 969, 979, 989, 999]
2017/2/14
Python学习札记(二十二) 函数式编程3 filter & SyntaxError: unexpected EOF while parsing的更多相关文章
- Python学习札记(三十二) 面向对象编程 Object Oriented Program 3
参考:访问限制 NOTE 1.eg. #!/usr/bin/env python3 class Student(object): """docstring for Stu ...
- Python学习札记(四十二) IO 2
参考:StringIO和BytesIO NOTE 1.StringIO: 顾名思义就是在内存中读写str. #!/usr/bin/env python from io import BytesIO a ...
- Python学习札记(三十九) 面向对象编程 Object Oriented Program 10
参考:使用枚举类 NOTE #!/usr/bin/env python3 from enum import Enum def main(): Mouth = Enum('Mouth', ('Jan', ...
- Python学习札记(三十八) 面向对象编程 Object Oriented Program 9
参考:多重继承 NOTE #!/usr/bin/env python3 class Animal(object): def __init__(self, name): self.name = name ...
- Python学习札记(三十六) 面向对象编程 Object Oriented Program 7 __slots__
参考:slots NOTE 1.动态语言灵活绑定属性及方法. #!/usr/bin/env python3 class MyClass(object): def __init__(self): pas ...
- Python学习札记(三十五) 面向对象编程 Object Oriented Program 6
参考:实例属性和类属性 NOTE Python是动态语言,根据类创建的实例可以任意绑定属性. class Student(object): def __init__(self, name): self ...
- Python学习札记(三十四) 面向对象编程 Object Oriented Program 5
参考:获取对象信息 NOTE 1.type()函数可以用来判断对象的类型: >>> type(123) <class 'int'> >>> type(' ...
- Python学习笔记(四)函数式编程
高阶函数(Higher-order function) Input: 1 abs Output: 1 <function abs> Input: 1 abs(-10) Output: 1 ...
- Python学习笔记(十一)—— 函数式编程
一.函数式编程理念 函数式编程就是一种抽象程度很高的编程范式,纯粹的函数式编程语言编写的函数没有变量,因此,任意一个函数,只要输入是确定的,输出就是确定的,这种纯函数我们称之为没有副作用.而允许使用变 ...
随机推荐
- JQuery中$.ajax()方法参数详解 转载
url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. type: 要求为String类型的参数,请求方式(post或get)默认为get.注意其他http请求方法,例如put和 ...
- node中的对象
1. class的概念 定义一个class,属性都是private,方法都是public. Hello.js: 使用class index.js: 2. 单例类 使用exports而不是module. ...
- 【UOJ274】【清华集训2016】温暖会指引我们前行 LCT
[UOJ274][清华集训2016]温暖会指引我们前行 任务描述 虽然小R住的宿舍楼早已来了暖气,但是由于某些原因,宿舍楼中的某些窗户仍然开着(例如厕所的窗户),这就使得宿舍楼中有一些路上的温度还是很 ...
- Python 之定时器
#引入库 threading import threading #定义函数 def fun_timer(): print('hello timer') #打印输出 global timer #定 ...
- nginx_log介绍和分割
nginx access_log日志简介 log_format 日志格式 1.语法:log_format name(格式名字) 格式样式(即想要得到什么样的日志内容)示例: log_format ma ...
- pta习题集5-16 朋友圈
某学校有N个学生,形成M个俱乐部.每个俱乐部里的学生有着一定相似的兴趣爱好,形成一个朋友圈.一个学生可以同时属于若干个不同的俱乐部.根据"我的朋友的朋友也是我的朋友"这个推论可以得 ...
- Oracle性能优化之表压缩及并行提高效率的测试
1.制作测试表 create table t1 as select * from FW_T_GTXLOG insert into t1 select * from t1; create table t ...
- 'module' object has no attribute 'select'
和tensorflow的版本有关系 新版本 将tf.select替换为tf.where
- Django权限系统auth模块详解
转自:原文出处 auth模块是Django提供的标准权限管理系统,可以提供用户身份认证, 用户组和权限管理. auth可以和admin模块配合使用, 快速建立网站的管理系统. 在INSTALLED_A ...
- Flink简介及使用
一.Flink概述 官网:https://flink.apache.org/ mapreduce-->maxcompute HBase-->部门 quickBI DataV Hive--& ...