《Pyhon入门:07 map与filter函数的常用用法》

Pyhon入门之map与filter函数常用用法

    • [一、 map函数的常用用法](#一、 map函数的常用用法)
      • [1. 基本用法](#1. 基本用法)
      • [2. 使用lambda表达式](#2. 使用lambda表达式)
      • [3. 多个可迭代对象](#3. 多个可迭代对象)
      • [4. 使用自定义函数](#4. 使用自定义函数)
      • [5. 返回迭代器](#5. 返回迭代器)
      • [6. 与filter函数结合使用](#6. 与filter函数结合使用)
    • [二、 filter函数的常用用法](#二、 filter函数的常用用法)

一、 map函数的常用用法

1. 基本用法

map()函数是Python内置的一个函数,用于将一个函数应用于可迭代对象的每个元素,并返回一个迭代器。

python 复制代码
def square(n):
    return n * n

numbers = [1, 2, 3, 4, 5]
result = map(square, numbers)
print(list(result))  # 输出: [1, 4, 9, 16, 25]

2. 使用lambda表达式

map()函数可以与lambda表达式一起使用,以简化代码。

python 复制代码
numbers = [1, 2, 3, 4, 5]
result = map(lambda x: x * x, numbers)
print(list(result))  # 输出: [1, 4, 9, 16, 25]

3. 多个可迭代对象

map()函数可以接受多个可迭代对象作为参数,并将函数应用于每个可迭代对象的对应元素。

python 复制代码
numbers1 = [1, 2, 3, 4, 5]
numbers2 = [6, 7, 8, 9, 10]
result = map(lambda x, y: x + y, numbers1, numbers2)
print(list(result))  # 输出: [7, 9, 11, 13, 15]

4. 使用自定义函数

map()函数可以接受一个自定义函数作为参数,并将该函数应用于可迭代对象的每个元素。

python 复制代码
def add_one(n):
    return n + 1

numbers = [1, 2, 3, 4, 5]
result = map(add_one, numbers)
print(list(result))  # 输出: [2, 3, 4, 5, 6]

5. 返回迭代器

map()函数返回一个迭代器,而不是一个列表。这意味着你可以使用for循环来遍历结果,而不需要将其转换为列表。

python 复制代码
numbers = [1, 2, 3, 4, 5]
result = map(lambda x: x * x, numbers)
for num in result:
    print(num)  # 输出: 1, 4, 9, 16, 25

6. 与filter函数结合使用

map()函数可以与filter()函数结合使用,以过滤可迭代对象中的元素。

python 复制代码
numbers = [1, 2, 3, 4, 5]
result = map(lambda x: x * x, filter(lambda x: x % 2 == 0, numbers))
print(list(result))  # 输出: [4, 16]

以上是map()函数的常用用法。

二、 filter函数的常用用法

Python filter() 函数用于过滤序列,过滤掉不符合条件的元素,返回一个迭代器对象,如果要转换为列表,可以使用 list() 函数。

filter() 函数的基本语法如下:

python 复制代码
filter(function, iterable)
  • function:判断函数,接收一个参数,返回布尔值,TrueFalse
  • iterable:可迭代对象。

下面是一个使用 filter() 函数的例子,过滤出列表中的所有偶数:

python 复制代码
def is_even(n):
    return n % 2 == 0

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers = filter(is_even, numbers)
print(list(even_numbers))

在上面的代码中,is_even() 函数是一个判断函数,用于判断一个数是否为偶数。filter() 函数将 is_even() 函数和 numbers 列表作为参数,过滤出 numbers 列表中的所有偶数,返回一个迭代器对象。最后,使用 list() 函数将迭代器对象转换为列表。

另外,filter() 函数也可以使用匿名函数(lambda 函数)作为判断函数,例如:

python 复制代码
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers = filter(lambda x: x % 2 == 0, numbers)
print(list(even_numbers))

在上面的代码中,lambda x: x % 2 == 0 是一个匿名函数,用于判断一个数是否为偶数。filter() 函数将这个匿名函数和 numbers 列表作为参数,过滤出 numbers 列表中的所有偶数,返回一个迭代器对象。最后,使用 list() 函数将迭代器对象转换为列表。

以上是 Python filter() 函数的常用用法,包括使用判断函数和匿名函数作为参数,以及将过滤结果转换为列表。

相关推荐
2601_9623815841 分钟前
ArcGIS+Python+AI赋能高标准农田建设项目审计提质增效
python·arcgis·ai·高标准农田审计·大数据审计
rust&python1 小时前
课程表的归一化处理
python·程序人生·信息可视化
2601_962099082 小时前
replit如何运行python
python·代码编辑器·编程环境·在线ide·replit
头发够用的程序员2 小时前
别被 TOPS 参数迷惑:手把手推导边缘 GPU 整型算力
人工智能·python·ubuntu·计算机视觉·硬件架构·边缘计算
TechWayfarer2 小时前
做IP归属地查询时总是查不准?IP纯净度检测实战:从“查归属地“到识别“脏IP“的四个维度
服务器·网络·python·tcp/ip·安全·web安全
小溪学编程2 小时前
Java InputStream 详解:从基础到实战
java·python·php
笨笨饿3 小时前
#137_死机恢复现场_Armino平台AP系统swd调试
git·python·stm32·单片机·嵌入式硬件·物联网·vim
2601_962382433 小时前
python常用函数大全pdf-python函数大全.pdf
python·内置函数·io操作·集合操作·数学运算
奈斯先生Vector3 小时前
AIGC 视频生成实战:用 Kling Video 拆解文生视频、图生视频与完整工作流
开发语言·人工智能·windows·python·aigc·音视频
小玮看世界3 小时前
[Python]螺旋遍历 vs 最短路径:方向控制类算法的“同源异流”
开发语言·python·算法