《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() 函数的常用用法,包括使用判断函数和匿名函数作为参数,以及将过滤结果转换为列表。

相关推荐
YiSLWLL7 分钟前
使用Tauri 2.3.1+Leptos 0.7.8开发桌面小程序汇总
python·rust·sqlite·matplotlib·visual studio code
花酒锄作田40 分钟前
[flask]自定义请求日志
python·flask
SsummerC2 小时前
【leetcode100】组合总和Ⅳ
数据结构·python·算法·leetcode·动态规划
Tandy12356_2 小时前
Godot开发2D冒险游戏——第一节:主角登场!
python·游戏引擎·godot
西柚小萌新3 小时前
【Python爬虫基础篇】--4.Selenium入门详细教程
爬虫·python·selenium
橘猫云计算机设计4 小时前
springboot基于hadoop的酷狗音乐爬虫大数据分析可视化系统(源码+lw+部署文档+讲解),源码可白嫖!
数据库·hadoop·spring boot·爬虫·python·数据分析·毕业设计
YOULANSHENGMENG4 小时前
linux 下python 调用c++的动态库的方法
c++·python
SsummerC4 小时前
【leetcode100】零钱兑换Ⅱ
数据结构·python·算法·leetcode·动态规划
一眼青苔5 小时前
切割PDF使用python,库PyPDF2
服务器·python·pdf
电商数据girl5 小时前
产品经理对于电商接口的梳理||电商接口文档梳理与接入
大数据·数据库·python·自动化·产品经理