zoukankan      html  css  js  c++  java
  • 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']
    
  • 相关阅读:
    第35条:注解优先于命名模式
    Apache Shiro入门实例
    第34条:用接口模拟可伸缩的枚举
    Lua数组排序
    C++多态性的理解
    爱推软件
    VMProtect使用小计【一】
    android软件开发之webView.addJavascriptInterface循环渐进【二】
    cocos2dx如何添加popScene的场景动画
    Cocos2d-x 3.0 cocostudio骨骼动画的动态换肤
  • 原文地址:https://www.cnblogs.com/charon2/p/10375758.html
Copyright © 2011-2022 走看看