zoukankan      html  css  js  c++  java
  • 面试题-python3 内置函数map reduce filter 如何使用?

    前言

    面试时候经常会考到 map reduce filter 这三个内置函数的使用

    map() 函数

    map() 会根据提供的函数对指定序列做映射。
    第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函数返回值的新列表。
    map() 函数语法:map(function, iterable, ...)

    • function -- 函数
    • iterable -- 一个或多个可迭代对象

    Python 3.x 返回迭代器

    题1:有个列表a = [1, 2, 3, 4] 计算列表中每个数除以2 取出余数 得到 [1,0,1,0]

    a = [1, 2, 3, 4]
    # map使用
    def get_yushu(x):
        return x % 2
    print(map(get_yushu, a))  # map object
    print(list(map(get_yushu, a)))  # [1, 0, 1, 0]
    
    # 使用匿名函数
    print(list(map(lambda x: x%2, a)))
    

    题2: 请将列表 [1,2,3,4,5] 使用python方法转变成 [1,4,9,16,25]

    a = [1, 2, 3, 4, 5]
    # 计算平方的函数
    def seq(x):
        return x**2
    print(list(map(seq, a)))
    
    # 匿名函数
    print(list(map(lambda x: x**2, a)))
    
    

    使用总结:map函数的功能可以理解成,对可迭代对象中的成员分别做一个功能计算,得到一个新的可迭代对象

    题3:map函数对列表a=[1,3,5],b=[2,4,6]相乘得到[2,12,30]

    map函数是可以传多个可迭代对象的

    a = [1, 3, 5]
    b = [2, 4, 6]
    
    def chengji(x, y):
        return x*y
    print(list(map(chengji, a, b)))
    
    # 匿名函数
    print(list(map(lambda x, y: x*y, a, b)))
    

    reduce() 函数

    在 Python3 中,reduce() 函数已经被从全局名字空间里移除了,它现在被放置在 functools 模块里,如果想要使用它,则需要通过引入 functools 模块来调用 reduce() 函数
    使用语法:reduce(function, sequence, initial=None)
    参数:

    • function 调用的function必须有2个参数
    • sequence 传一个序列
    • initial 是初始值,默认为None

    例如:reduce(lambda x,y:x+y,[1,2,3,4,5])计算((((1+2)+3)+4)+5)

    题4: 计算1-100的和

    from functools import reduce
    
    
    def add(x, y):
        return x + y
    print(reduce(add, range(1, 101)))
    
    # 也可以用匿名函数
    print(reduce(lambda x,y: x+y, range(1, 101)))
    

    题5:reduce函数计算10!

    # reduce 计算10!
    from functools import reduce
    
    print(reduce(lambda x, y: x*y, range(10, 0, -1)))
    

    题6:计算1!+2!+3!+。。。+10!

    from functools import reduce
    
    # # i的阶乘
    # i = 10
    # a = reduce(lambda x, y: x*y, range(1, i+1))
    
    # 先生成每个阶乘的列表
    x = [reduce(lambda x, y: x*y, range(1, i+1)) for i in range(1, 11)]
    b = reduce(lambda a,b: a+b, x)
    print(b)
    

    filter() 函数

    filter() 函数用于过滤序列,过滤掉不符合条件的元素,返回由符合条件元素组成的新列表。
    filter() 方法的语法: filter(function, iterable)
    参数

    • function -- 判断函数。
    • iterable -- 可迭代对象。

    返回filter object 迭代器对象

    题7:有个列表a = [1, 3, 5, 7, 0, -1, -9, -4, -5, 8] 使用filter 函数过滤出大于0的数

    a = [1, 3, 5, 7, 0, -1, -9, -4, -5, 8]
    def great_then_0(x):
        """判断大于0"""
        return x > 0
    print(filter(great_then_0, a))
    print(list(filter(great_then_0, a)))
    

    题8:列表b = ["张三", "张四", "张五", "王二"] 过滤掉姓张的姓名

    def remove_zhang(x):
        return not str(x).startswith("张")
    print(list(filter(remove_zhang, b)))
    
    # 也可以用lambda
    print(list(filter(lambda x: not str(x).startswith("张"), b)))
    

    题9:过滤掉列表中不及格的学生
    a = [
    {"name": "张三", "score": 66},
    {"name": "李四", "score": 88},
    {"name": "王五", "score": 90},
    {"name": "陈六", "score": 56},
    ]

    # 过滤掉列表中不及格的学生
    a = [
            {"name": "张三", "score": 66},
            {"name": "李四", "score": 88},
            {"name": "王五", "score": 90},
            {"name": "陈六", "score": 56},
         ]
    
    # 返回
    print(list(filter(lambda x: x.get("score")>=60, a)))
    
    
  • 相关阅读:
    我(webabcd)的文章索引
    学习重构的一些思考
    学习重构的一些思考
    软件编程思想读后感
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
  • 原文地址:https://www.cnblogs.com/yoyoketang/p/14584569.html
Copyright © 2011-2022 走看看