Python有几个相对特殊的函数,他们并不会提高工作效率,但是会使代码优雅简洁,其中包括lambda, map, reduce, filter, yeild。

第一:lambda,贴些代码体会。

 1 #lambda 函数
2 def add(x):
3 x += 3
4 return x
5
6 lam = lambda x:x+3
7 numbers = list(range(10))
8 newNumbers1 = []
9 newNumbers2 = []
10 newNumbers3 = []
11 newNumbers4 = []
12 newNumbers5 = []
13
14 for i in numbers:
15 newNumbers1.append(add(i))
16 print("New Numbers 1: \n\t", newNumbers1);
17
18 newNumbers2 = [i+3 for i in numbers]
19 print("New Numbers 2: \n\t", newNumbers2)
20
21 newNumbers3 = [add(i) for i in numbers]
22 print("New Numbers 3: \n\t", newNumbers3)
23
24 newNumbers4 = [lam(i) for i in numbers]
25 print("New Numbers 4: \n\t", newNumbers4)
26
27 for i in numbers:
28 newNumbers5.append(lam(i))
29 print("New Numbers 5: \n\t", newNumbers5)

lambda总结:

  • 在lambda后面直接跟变量
  • 变量后面是冒号
  • 冒号后面是表达式

第二是map()函数,map函数是python的内置函数。官方说明: 

map(func, *iterables) --> map object
|
| Make an iterator that computes the function using arguments from
| each of the iterables. Stops when the shortest iterable is exhausted.

个人理解:基本样式:map(function, sequence),function是函数,sequence是序列对象,在执行的过程中,序列对象中的每个元素,按照从左到右的顺序,依次被提取出来,并作为实参传递给function函数,然后将function的返回值依次存到一个列表中.

 1 #延续上面的例子
2 #定义函数add
3 def add(x):
4 x += 3
5 return x
6 lam = lambda x:x+3 #定义lambda函数并赋值给lam
7 numbers = range(10) #定义numbers
8
9 newNumber1 = map(add, numbers) #调用map函数将序列numbers中的每一个元素从左到右提取出来,并作为实参传递给函数add,然后将函数add的返回值存储在一个列表中。
10 print("New number is: \n\t", list(newNumber1)) #打印看是否符合预期
11
12 newNumber2 = map(lam, numbers); #调用map函数将序列numbers中的每一个元素从左到右提取出来,并作为实参传递给函数lambda,然后将函数lambda的返回值存储在一个列表中。
13 print("New Number 2 is: \n\t", list(newNumber2)); #打印看是否符合预期

再来一个计算平方根的例子,一般是如下用循环:

1 items = [1,2,3,4,5] #定义列表
2 square1 = [] #定义空列表,以存储计算后的平方数
3 #循环读取i在列表items中的值
4 for i in items:
5 square1.append(i ** 2) #将每次循环所得的平方数追加到列表
6 print("Results is: \n\t", square1) #打印看是否符合预期:[1, 4, 9, 16, 25]

下面用map来实现:

 1 def sqr(x):
2 return x ** 2
3 #用map实现
4 square2 = map(sqr, items)
5 print("Results is: \n\t", list(square2))
6
7 #也可以用map配合lambda实现
8 square3 = map((lambda x:x**2), items)
9 print("Results is: \n\t", list(square3))
10 #也可以用列表解析
11 square4 = [x**2 for x in items]
12 print("Square 4 is: \n\t", square4)

再来一个计算列表元素之和的例子,比如有两个列表,要计算他们的和然后再返回一个列表,就可以用map优雅简洁的实现:

 1 list1 = [1,2,3,4,5]
2 list2 = [6,7,8,9,10]
3
4 sumList = map((lambda x,y:x+y), list1,list2) #调用map函数将序列list1和list2中的每一个值从左到右提取出来,并作为实参传递给lambda函数,然后将返回的值存储在一个列表中
5 print("The result is: \n\t", list(sumList))
6
7 lst1 = [1,2,3,4,5]
8 lst2 = [6,7,8,9,10]
9 lst3 = [11,12,13,14,15]
10 sumList = map((lambda x,y,z:x+y+z), lst1,lst2,lst3)
11 print("The result is: \n\t", list(sumList))

第三个函数是reduce(),reduce也给一个官方的说明:

reduce(function, sequence[, initial]) -> value

Apply a function of two arguments cumulatively to the items of a sequence,
from left to right, so as to reduce the sequence to a single value.
For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
((((1+2)+3)+4)+5). If initial is present, it is placed before the items
of the sequence in the calculation, and serves as a default when the
sequence is empty.
(END)

reduce在我这里的应用可能就一个计算列表的总和了。毕竟能力有限。还是从数字列表理解一下,有一个列表:【1,2,3,4,5】,要计算元素的总和。简单的就sum(list)。用reduce也可以,他的方法是,先提取最左边的两个元素相加,然后把结果和第三个元素再相加,然后把结果再和第四个元素相加,以此类推。求列表的和,有一篇文章专门讲述了 - Python求列表元素之和。这里是说reduce:

 1 lst = [1,2,3,4,5]
2 def sumList(lst):
3 return sum(lst)
4 print("Sum is: \n\t", sumList(lst))
5
6 #使用for循环
7 sumList = 0 #定义和变量,初始为0
8 for i in lst:
9 sumList += i
10 print("Sum is: \n\t", sumList)
11
12 #for循环2
13 sumList = 0
14 for i in range(len(lst)):
15 sumList += lst[i]
16 print("Sum is: \n\t", sumList)
17
18 #使用reduce函数
19 sumList = reduce((lambda x,y:x+y), lst); #调用reduce函数对序列lst中的每个元素进行相加
20 print(sumList)

使用reduce计算两个列表对应元素的乘积和。假设有两个列表a = [3,9,8,5,2],b=[1,4,9,2,6]。现在要计算a[0]*b[0]+a[1]*b[1]+...+a[n]*b[n]。先用zip将列表a和列表b打包成一个个元组,然后再用列表解析成一个新列表,然后计算新列表的和。

1 a = [3,9,8,5,2] #定义列表a
2 b = [1,4,9,2,6] #定义列表b
3
4 #调用zip将a和b中的对应的元素打包成一个个元组,然后返回由这些元组组成的列表
5 newListc = [x * y for x,y in zip(a,b)]; #通过列表解析将对应的值赋值给xy
6 print(newListc) #打印输出:[3, 36, 72, 10, 12]
7 print(sum(newListc)) #得到要求的数据:133

也可以用reduce:

1 a = [3,9,8,5,2] #定义列表a
2 b = [1,4,9,2,6] #定义列表b
3 #使用reduce函数
4 newListreduce = reduce(lambda x,y:x+y, map(lambda x,y: x*y, a,b))
5 print(newListreduce) #133

第四个函数是filter函数,中文理解就是过滤器。贴官方说明:

filter(function or None, iterable) --> filter object
|
| Return an iterator yielding those items of iterable for which function(item)
| is true. If function is None, return the items that are true.

1 numbers = list(range(-5,5)) #定义数字列表
2 print(numbers); #打印[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4]
3 newNumbers1 = filter(lambda x: x>0, numbers) #调用filter函数进行过滤
4 print(list(newNumbers1)) #打印输出:[1, 2, 3, 4]
5
6 newNumbers2 = [x for x in numbers if x>0] #列表解析
7 print(newNumbers2) #打印输出:[1, 2, 3, 4]
8
9 baidu = filter(lambda x : x!='a', 'baidu.com') #bidu.com

还有个一yield,戳这里。

最新文章

  1. H5新出的flex布局
  2. hdu 4038 2011成都赛区网络赛H 贪心 ***
  3. iOS Error
  4. 基于FreeBSD 64位内核的kFreeBSD无法在Virtualbox下安装
  5. highcharts 设置标题不显示
  6. 操作系统之进程篇(4)--经典进程间通信(IPC)问题
  7. 获取context path或者basePath
  8. UITabBar背景、icon图标颜色、被选中背景设置以及隐藏UITabBar的两种方式
  9. 手机SIM卡知识大科普
  10. AFNetworking 3.0x版本最新特性
  11. 洛谷 P2491 解题报告
  12. sql 参数化查询问题
  13. padding内边距
  14. English-商务英文邮件例句100句
  15. JAVA(一)JAVA基础/面向对象基础/高级面向对象
  16. 分页器的js实现代码 bootstrap Paginator.js
  17. ARM体系结构和汇编指令
  18. Framebuffer 驱动学习总结(一) ---- 总体架构及关键结构体
  19. HDUOJ--1865 1string
  20. 20145307第三次JAVA学习实验报告

热门文章

  1. iOS7使用iOS8上的方法报错处理
  2. Redis设计与实现一之简单的动态字符串
  3. Spring Boot第一天
  4. Sysbench对Mysql进行基准测试
  5. kafka 数据存储和发送
  6. 5. Idea集成Git
  7. 网骗欺诈?网络裸奔?都是因为 HTTP?
  8. Docker中搭建FastDFS文件系统(多图)
  9. Verilog单周期CPU(未完待续)
  10. 饱含辛酸开发 WPF CustomControl