python常用高阶函数

map函数

  • map(function, iterable1[, iterable2, …])
    map() 是 Python 内置的高阶函数,它用于将一个或多个可迭代对象(如列表、元组、字符串等)中的每个元素按照指定的函数进行处理,并返回一个迭代器(iterator),其生成结果为经过函数映射后的元素序列。

    function: 必需参数,指定应用于每个元素的函数。它可以是用户定义的函数、内置函数、或者使用 lambda 表达式创建的匿名函数。
    iterable1, iterable2, …: 可选参数,一个或多个可迭代对象。map() 将对这些对象中对应的元素逐个应用
    注:当提供多个可迭代对象时,它们的长度必须相等,function 将按位置分别接收每个可迭代对象对应位置的元素作为参数。

    返回值
    map() 函数返回一个迭代器对象。通过迭代(使用 for 循环、list() 函数、tuple() 函数等)可以获取映射结果的实际序列。

vec = [1, 2, 3, 4, 5]
print(list(map(lambda v: str(v), vec)))
# ['1', '2', '3', '4', '5']

numbers1 = [10, 20, 30]
numbers2 = [5, 6, 7]
products = list(map(lambda x, y: x * y, numbers1, numbers2))
print(products)
# [50, 120, 210]

filter函数

  • filter(function, iterable)

    filter() 是 Python 内置的高阶函数,用于对一个可迭代对象(如列表、元组、字符串等)进行筛选,保留那些使指定函数返回值为True的元素,并返回一个迭代器(iterator),其中包含筛选后的元素序列。

even_numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# 保留为True的结果
even_filter = filter(lambda x: x % 2 == 0, even_numbers)
print(list(even_filter))
# [2, 4, 6, 8, 10]

zip函数

  • zip()
    将多个可迭代对象的元素打包成一个元组,返回一个迭代器。当各个可迭代对象长度不一致时,以最短的对象为准。
str_list = ["one", "two", "three"]
num_list = [1, 2]
ret = zip(str_list, num_list)  # 不会报错,直接结束
print(dict(ret))
# {'one': 1, 'two': 2}

str_list = ["one", "two", "three"]
num_list = [1, 2, 3]
for i in zip(str_list, num_list):
    print(i)  # ('one', 1)
ret_list = list(zip(str_list, num_list))
ret_dict = dict(zip(str_list, num_list))
print(ret_list)
# [('one', 1), ('two', 2), ('three', 3)]
print(ret_dict)
# {'one': 1, 'two': 2, 'three': 3}

numbers1 = [10, 20, 30]
for index, value in enumerate(numbers1):
    print(index, "=", value)

for index, value in enumerate(numbers1, start=1):  # 自定义index
    print(index, "=", value)

enumerate函数

  • enumerate(iterable, start=0)
    返回一个迭代器,生成 (index, value) 元组序列,其中 index 是从 start 开始递增的整数,value 是来自 iterable 的相应元素。
nums = ["a", "b", "c"]
for index, value in enumerate(nums):
    print(index, "=", value)
# 0 = a
# 1 = b
# 2 = c

for index, value in enumerate(nums, start=1):  # 自定义index
    print(index, "=", value)
# 1 = a
# 2 = b
# 3 = c

list(enumerate(nums))   # 将索引和值zip成元组
# [(0, 'a'), (1, 'b'), (2, 'c')]

sorted函数

  • sorted(iterable, *, key=None, reverse=False)
    接收一个可迭代对象,返回一个排序后的列表。可选参数 key 指定一个函数,用于提取每个元素用于排序的值;reverse 参数为 True 时,表示降序排列。

    iterable:必选参数,指定要排序的可迭代对象。它可以是任何可迭代的数据结构,如列表、元组、字符串、字典视图(如 .keys()、.values() 或 .items())、文件对象、生成器表达式等。
    key:可选参数,用于指定一个函数(或可调用对象),该函数接收一个元素作为参数,并返回一个值作为排序依据。sorted 函数会根据这个返回值对元素进行排序。如果不指定 key,则默认按元素本身的值进行比较排序。
    reverse:可选参数,布尔类型,表示是否逆序排列。当 reverse=True 时,sorted 函数将返回降序排列的结果;默认值为 False,此时返回升序排列的结果。

numbers = [3, 1, 4, 2, 5]
sorted_numbers = sorted(numbers)
print(sorted_numbers)
# [1, 2, 3, 4, 5]

# 复杂数据
data = [("验", 1), ("证", 2), ("码", 3)]
# 过滤按元组的第二个元素进行降序
print(sorted([_ for _ in data if _[1] >= 1], key=lambda x: x[1], reverse=True))
# [('码', 3), ('证', 2), ('验', 1)]
  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值