博客
关于我
Python函数,匿名函数,高阶函数,内置函数——08
阅读量:799 次
发布时间: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 一个list去除另一个list中的值
    查看>>
    python 三大框架的 介绍。
    查看>>
    Python 下载的 11 种姿势,一种比一种高级!
    查看>>
    python读取一个文件夹下所有图片_初学Python-找出文件夹下的所有图片
    查看>>
    Python 中 3 个不可思议的返回功能
    查看>>
    python 中 dict 的另一种用法
    查看>>
    Python 中 PIL 读取图片出现异常旋转的解决方法
    查看>>
    python 中os.path.join 双斜杠的解决办法
    查看>>
    Python 中Semaphore 信号量对象、Event事件、Condition
    查看>>
    python 中with的使用及样例
    查看>>
    Python 中内置的最大堆 API
    查看>>
    Python 中只有一个 True 和一个 False 对象吗?
    查看>>
    python读取mtcars数据集并实现以下操作_关于数据处理。。,Python交流,技术交流区,鱼C论坛 - Powered by Discuz!...
    查看>>
    Python 中多线程与多处理之间的区别
    查看>>
    Python 中如何使用 lambda 函数
    查看>>
    Python 中如何创建多行字符串?
    查看>>
    Python 中如何处理异常?
    查看>>
    Python 中如何实现列表的切片?
    查看>>
    Python 中如何实现字典的排序?
    查看>>