zoukankan      html  css  js  c++  java
  • Python -- lambda, map, filter


    lambda

    f = lambda x : x * 2

    f(5)


    f = lambda x,y,z : x+y+z

    f(2,1,3)

    map

    list(map(lambda x:x[0].upper()+x[1:].lower(), ['sQd', 'ZORO']))

    #传入列表,首字母变大写,其余变小写

    reduce

    from functools import reduce

    def add(x, y):
      return x + y

    reduce(add, [1,2,3,4])
    reduce(f, [x1, x2, x3, x4]) = f(f(f(x1, x2), x3), x4)

    filter

    list(filter(lambda n: n%2 == 1, [1,2,3,4,5]))

    #保留奇数,舍弃偶数
    list(filter(lambda s: s and s.strip(), ['S', '', None, 'b']))

    #删除一个列表中的空元素

    KEEP LEARNING!
  • 相关阅读:
    Spring MVC(一)
    Spring-IOC总结
    IT
    Spring基础
    Maven
    Ajax笔记
    数据库和SQL语言
    JDBC
    拦截器
    文件上传
  • 原文地址:https://www.cnblogs.com/roronoa-sqd/p/4899175.html
Copyright © 2011-2022 走看看