• day16 Python filter函数


    前戏

    movie_people = ["alex","charon","pluto","liu","sb","sb_250"]
    
    ret = []
    for i in movie_people:
        if not i.startswith("sb"):
            ret.append(i)
    
    print(ret)
    
    结果:
    ['alex', 'charon', 'pluto', 'liu']
    

     前五分钟

    movie_people = ["alex","charon","pluto","liu","sb","sb_250"]
    
    def filter_test(arrat):
        ret = []
        for i in arrat:
            if not i.startswith("sb"):
                ret.append(i)
        return ret
    
    print(filter_test(movie_people))
    
    结果:
    ['alex', 'charon', 'pluto', 'liu']
    

     十分钟

    movie_people=['alex_sb','wupeiqi_sb','linhaifeng','yuanhao_sb']
    def sb_show(n):
        return n.endswith('sb')
    
    def filter_test(func,array):
        ret=[]
        for p in array:
            if not func(p):
                   ret.append(p)
        return ret
    
    res=filter_test(sb_show,movie_people)
    print(res)
    
    结果:
    linhaifeng
    

     十五分钟

    movie_people=['alex_sb','wupeiqi_sb','linhaifeng','yuanhao_sb']
    # def sb_show(n):
    #     return n.endswith('sb')
    #--->lambda n:n.endswith('sb')
    #  :前面是参数,后面是返回值
    def filter_test(func,array):
        ret=[]
        for p in array:
            if not func(p):
                   ret.append(p)
        return ret
    
    res=filter_test(lambda n:n.endswith('sb'),movie_people)
    print(res)
    
    #filter函数
    movie_people=['alex_sb','wupeiqi_sb','linhaifeng','yuanhao_sb']
    print(filter(lambda n:not n.endswith('sb'),movie_people))
    
    
    
    res=filter(lambda n:not n.endswith('sb'),movie_people)
    print(list(res))
    
    
    print(list(filter(lambda n:not n.endswith('sb'),movie_people)))
    
    结果:
    ['linhaifeng']
    <filter object at 0x7f9aa857b908>
    ['linhaifeng']
    ['linhaifeng']
    
  • 相关阅读:
    GITHUB随笔 15-5月 junit
    github心得
    (转)哪个瞬间让你突然觉得逛知乎真有用?
    0-1背包问题(the knapsack problem)
    对VSCode在安装了Dev-cpp的电脑上的配置
    非全日制考研资料(未完待续)
    CSS那些事读书笔记-2
    CSS那些事读书笔记-1
    前端面试准备-1
    工作日记-一个有趣的定时任务实现
  • 原文地址:https://www.cnblogs.com/charon2/p/10375758.html
Copyright © 2020-2023  润新知