lambda&map

lambda

    匿名函数:

An anonymous inline function consisting of a single expression which is evaluated when the function is called. The syntax to create a lambda function is lambda [parameters]: expression
关键字lambda表示匿名函数,冒号前面的x表示函数参数;
匿名函数限制:只能有一个表达式:consiting of a single expression,不需要return返回表达式的结果。

    优势:1 不需要定义函数,直接使用lambda

               2 因为函数没用名字,不用担心函数名冲突

               3 匿名函数也是一个函数对象,可以把匿名函数赋值给一个变量,再利用该变量来调用函数

 e.g:   

a = lambda x,y: x+y
print(a(3, 4))

  执行结果是:7

map 

  函数功能:

  python2里,直接使用map就能打印结果,但是python3中,map返回的结果是迭代器(iterator),因此需要转成list,再print输出。

  python2:

  map被定义成一个函数,返回值是一个列表

 

 python3:

  map被定义成一个类,返回值是一个迭代器

 

 

  功能依旧是将function作用于要被遍历的序列,但是最后返回的结果就是一个对象了;但是看功能描述,python2中和python3中的map还是有许多不同之处的。

  优势,如果对于较大的序列,python3中map将比python2中的开销更小

e.g:

  

 1 def ccc(x, y, z):
 2     return x, y, z
 3 
 4 list1 = [1, 2, 3]
 5 list2 = [1, 2, 3, 4]
 6 list3 = [1, 2, 3, 4, 5]
 7 res = map(ccc, list3, list2, list1)
 8 print(list(res))
 9 
10 输出结果:
11     [(1, 1, 1), (2, 2, 2), (3, 3, 3)]
12 
13 
14 ####但是在python2中将输出[(1, 1, 1), (2, 2, 2), (3, 3, 3), (None, 4, 4), (None, None, 5)]

 

  

e.g:

  英文名字首字母大写,其余小写

  

1 def Username(s):
2     name = s[0].upper() + s[1:].lower()
3     return name
4 
5 print(list(map(Username, ['adam', 'LISA', 'barT'])))
6 #注意map(func, sequence)  中是写的函数名,不是func()
7 
8 #输出:
9     ['Adam', 'Lisa', 'Bart']

 

lambda 在高阶函数中的用法:

     map    

  语法:map(function, iterable, ...)
  描述:根据提供的函数对序列做映射,返回迭代器

  "Make an iterator that computes the function using arguments fromeach of the iterables. Stops when the shortest iterable is exhausted."

  e.g:

print(list(map(lambda x, y: x+y, [1, 2, 3, 4],[1, 1, 1, 1, 1, 1])))
#输出结果:
    [2, 3, 4, 5]

     reduce

   语法:reduce(function, iterable[, initializer])

   描述:reduce函数会对参数中的序列进行累积,返回计算结果。 

    在functools中

  """
  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.
"""

  e.g:

1 from functools import reduce
2 def adding(x,y):
3     return x+y
4 
5 
6 print(reduce(adding,[ 1,2,3]))
7 print(reduce(lambda x, y: x+y, [ 1,2,3]))
8 #输出全是 6
print(reduce(lambda x, y: x*10+y, [1, 3, 5, 7, 9]))

#输出:13579
"第一次:1,3代入得13,第二次:13,5代入135"

    filter

    语法:fliter(func,literable)

    描述:函数用于过滤序列,得到未被过滤掉的元素得到新的迭代对象。

    """

     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.

    """

  e.g:

    

1 def is_odd(n):
2     return n % 2 == 0
3 
4 
5 list1= filter(is_odd, [1, 2, 3, 4, 5, 6, 7, 8, 9])
6 print(list(list1))
7 
8 #输出结果:
9     [2, 4, 6, 8]

 

print(list(filter(lambda x: x % 2 == 0, [1, 2, 3, 4, 5, 6, 7, 8, 9])))
#输出结果:[2, 4, 6, 8]

  

 

 

  

 

 

 

 

 

 

 

 

 

posted @ 2021-08-04 20:57  甲壳虫~~~  阅读(88)  评论(0)    收藏  举报