博客
关于我
Python函数,匿名函数,高阶函数,内置函数——08
阅读量:798 次
发布时间:2023-03-07

本文共 3333 字,大约阅读时间需要 11 分钟。

Python 函数与高阶函数

函数基础

当一个函数的返回值是另一个函数的函数名时,只是返回该函数的内存地址,该函数的作用域不会发生改变。

name = 'winsodm'
def test():
name = 'xl'
print('in the test')
def test1():
print('in the test1')
return test
res = test1()
# 返回的是 test1 中的函数名,函数的作用域不会改变

匿名函数

lambda 关键字用于定义匿名函数。

格式

lambda 形参:return值

示例

lambda x: x + 1
lambda x, y, z: (x + 1, y + 1, z + 1)

匿名函数使用后会自动释放内存空间。

高阶函数

高阶函数的定义:

  • 将函数作为参数传递给另一个函数(函数接收的参数是一个函数名)
  • 返回值中包含函数
  • 示例

    def test():
    name = 'xl'
    print('in the test')
    def test1():
    print('in the test1')
    return test

    高阶函数示例

    def test():
    name = 'xl'
    print('in the test')
    def test1():
    print('in the test1')
    return test

    map内置函数

    map 函数用于将一个函数应用到可迭代对象的每个元素上。

    示例

    num_l = [1, 3, 5, 2, 10, 4, 6, 9, 8]
    new_num_l = list(map(lambda x: x + 1, num_l))
    # 结果:new_num_l = [2, 4, 6, 3, 11, 5, 7, 10, 9]

    自己实现 map

    num_l = [1, 3, 5, 2, 10, 4, 6, 9, 8]
    def add_one(x):
    return x + 1
    def map_1(func, l):
    new_num_l = []
    for i in l:
    new_num_l.append(func(i))
    return new_num_l
    new_num_l = map_1(add_one, num_l)
    # 结果:new_num_l = [2, 4, 6, 3, 11, 5, 7, 10, 9]

    filter内置函数

    filter 函数用于过滤可迭代对象。

    示例

    name_list = ['xlc', 'hhc', 'hzzc', 'hc', 'winsdom']
    print(list(filter(lambda x: x.endswith('m'), name_list)))
    # 结果:['winsdom']

    自己实现 filter

    name_list = ['xlc', 'hhc', 'hzzc', 'hc', 'winsdom']
    def end_with(x):
    return x.endswith('c')
    def filter_1(func, array):
    new_name_list = []
    for n in array:
    if not func(n):
    new_name_list.append(n)
    return new_name_list
    new_name_list = filter_1(end_with, name_list)
    # 结果:new_name_list = ['winsdom']

    reduce内置函数

    reduce 函数用于将可迭代对象压缩成一个值。

    示例

    from functools import reduce
    num_l = [1, 3, 5, 100]
    num = reduce(lambda x, y: x * y, num_l, 2)
    # 结果:num = 3000

    自己实现 reduce

    from functools import reduce
    num_l = [1, 3, 5, 100]
    def reduce_1(func, array, init=None):
    if init == None:
    res = array.pop(0)
    else:
    res = init
    for i in array:
    res = func(res, i)
    return res
    res = reduce_1(lambda x, y: x * y, num_l, 100)
    # 结果:res = 3000

    其他函数

    bool 布尔值判断

    bool(0)  # 结果:False

    abs 取绝对值

    print(abs(-1))  # 结果:1

    all

    all 会将所有元素都取 bool 运算,只要有一个为假,结果为 False。

    print(all([1, 4, '2']))  # 结果:True

    any

    any 如果有一个为真,结果为 True。

    any([1, 0, '2'])  # 结果:True

    bin 二进制转换

    print(bin(3))  # 结果:0b11

    hex 十六进制转换

    print(hex(12))  # 结果:0xc

    oct 八进制转换

    print(oct(2))  # 结果:0o2

    bytes 转换为 bytes 类型

    name = '你好'
    print(bytes(name, encoding='utf-8')) # 结果:b'\xe4\xbd\xa0\xe5\xa5\xbd'
    print(bytes(name, encoding='utf-8').decode('utf-8')) # 结果:‘你好’

    chr 和 ord 转换

    print(chr(93))  # 结果:]

    dir 打印对象方法

    print(dir(all))  # 结果:['__call__', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__self__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__text_signature__']

    eval 表达式评估

    dic = {'name': 'winsdom'}
    dic_s = str(dic)
    eval(dic_s) # 可以将字符串中的数据结构提取出来
    express = '1 + 2 * (3 / 3 - 1) - 2'
    eval(express) # 可以把字符串中的表达式进行运算

    divmod 除法分割

    print(divmod(10, 3))  # 结果:(3, 1)

    hash 可hash 数据类型

    isinstance(1, int)  # True

    globals 和 locals

    print(globals())  # 打印全局变量
    print(locals()) # 打印局部变量

    转载于:https://www.cnblogs.com/winsdom/p/9102425.html

    你可能感兴趣的文章
    python 时间函数小总结
    查看>>
    Python 是否优化尾递归?
    查看>>
    Python 更新set
    查看>>
    Python 最强 IDE 详细使用指南!
    查看>>
    Python 有一个不可变的列表吗?
    查看>>
    Python 机器学习入门之 pandas 的使用
    查看>>
    Python 机器学习实战
    查看>>
    python 杀死子进程_subprocess.popen.kill杀死所有子进程
    查看>>
    Python 条件语句与循环结构详解
    查看>>
    Python 条件运算符解决方法如何工作?
    查看>>
    python 枚举类型
    查看>>
    Python 查询 Mongodb数据库
    查看>>
    python 查询Neo4j多节点的多层关系
    查看>>
    Python多处理导致许多僵尸进程
    查看>>
    Python多处理对象引用
    查看>>
    Python多处理安全地写入文件
    查看>>
    python多处理参数:深拷贝?
    查看>>
    Python多处理使用队列写入同一个文件
    查看>>
    Python多处理.Process:从局部变量开始
    查看>>
    Python多处理-进程数
    查看>>