鱼C论坛

 找回密码
 立即注册
查看: 3320|回复: 0

[学习笔记] 021函数:lambda表达式

[复制链接]
发表于 2017-6-26 21:37:43 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
1.lambda表达式的作用
1)Python写一些执行脚本时,使用lambda就可以省下定义函数过程,比如说我们只是需要写个简单的脚本来管理服务器时间,我们就不需要专门定义一个函数然后再写调用,使用lambda就可以使得代码更加精简;
2)对于一些比较抽象并且整个程序执行下来只需要调用一两次的函数,有时候给函数起个名字也是比较头疼的问题,使用lambda就不需要考虑命名的问题了;
3)简化代码的可读性,由于普通的屌丝函数阅读经常要调到开头def定义部分,使用lambda函数就可以省去这样的步骤;
  1. >>> def ds(x):
  2.         return 2 * x + 1

  3. >>> ds(5)
  4. 11
  5. >>> lambda x : 2 * x + 1
  6. <function <lambda> at 0x013E5300>
  7. >>> g = lambda x : 2 * x + 1
  8. >>> g(5)
  9. 11
  10. >>> def add(x,y):
  11.         return x + y

  12. >>> add(3, 4)
  13. 7
  14. >>> lambda x, y : x + y
  15. <function <lambda> at 0x0229CDF8>
  16. >>> g = lambda x, y : x + y
  17. >>> g(3, 4)
  18. 7
复制代码

2.filter()——过滤器
  1. >>> help(filter)
  2. Help on class filter in module builtins:

  3. class filter(object)
  4. |  filter(function or None, iterable) --> filter object
  5. |  
  6. |  Return an iterator yielding those items of iterable for which function(item)
  7. |  is true. If function is None, return the items that are true.
  8. |  
  9. |  Methods defined here:
  10. |  
  11. |  __getattribute__(self, name, /)
  12. |      Return getattr(self, name).
  13. |  
  14. |  __iter__(self, /)
  15. |      Implement iter(self).
  16. |  
  17. |  __new__(*args, **kwargs) from builtins.type
  18. |      Create and return a new object.  See help(type) for accurate signature.
  19. |  
  20. |  __next__(self, /)
  21. |      Implement next(self).
  22. |  
  23. |  __reduce__(...)
  24. |      Return state information for pickling.
复制代码

filter()有两个参数,第一个参数可以是一个函数也可以是None对象,第二个参数是一个可迭代的数据;如果第一个参数是一个函数的话,则将第二个可迭代数据里的每一个元素作为函数的参数计算,把返回Ture的值筛选出来并成立一个列表;如果第一个参数为None,则将第二个参数里面True的值筛选出来。
  1. >>> filter(None, [1, 0, False, True])
  2. <filter object at 0x003BACF0>
  3. >>> list(filter(None, [1, 0, False, True]))
  4. [1, True]
复制代码

过滤奇数的普通写法:
  1. >>> def odd(x):
  2.         return x % 2

  3. >>> temp = range(10)
  4. >>> show = filter(odd, temp)
  5. >>> list(show)
  6. [1, 3, 5, 7, 9]
复制代码

使用lambda()函数的写法:
  1. >>> list(filter(lambda x : x % 2, range(10)))
  2. [1, 3, 5, 7, 9]
复制代码

3.map()——映射
  1. >>> help(map)
  2. Help on class map in module builtins:

  3. class map(object)
  4. |  map(func, *iterables) --> map object
  5. |  
  6. |  Make an iterator that computes the function using arguments from
  7. |  each of the iterables.  Stops when the shortest iterable is exhausted.
  8. |  
  9. |  Methods defined here:
  10. |  
  11. |  __getattribute__(self, name, /)
  12. |      Return getattr(self, name).
  13. |  
  14. |  __iter__(self, /)
  15. |      Implement iter(self).
  16. |  
  17. |  __new__(*args, **kwargs) from builtins.type
  18. |      Create and return a new object.  See help(type) for accurate signature.
  19. |  
  20. |  __next__(self, /)
  21. |      Implement next(self).
  22. |  
  23. |  __reduce__(...)
  24. |      Return state information for pickling.
复制代码

map()这个内置函数有两个参数,一个是函数,一个是可迭代的序列;功能是:将序列的每一个元素作为函数的参数进行运算加工,直到可迭代序列的每一个元素都加工完毕返回所以加工后的元素构成的新序列。
  1. >>> list(map(lambda x : x * 2, range(10)))
  2. [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
复制代码

评分

参与人数 2鱼币 +7 收起 理由
小甲鱼 + 4 支持楼主!
鱼小二 + 3

查看全部评分

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 14:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表