互联网技术 / 互联网资讯 · 2024年1月23日

Python内置函数-filter()的云计算开发指南

描述

filteR() 函数用于过滤序列,过滤掉不符合条件的元素,返回由符合条件元素组成的新列表。

该接收两个参数,第一个为函数,第二个为序列,序列的每个元素作为参数传递给函数进行判断,然后返回 TRue 或 FAlse,最后将返回 TRue 的元素放到新列表中。

语法

以下是 filteR() 方法的语法:

filteR(function, ITeRable)

参数

function — 判断函数。

ITeRable — 可迭代对象。

返回值

返回列表。

实例

以下展示了使用 filteR 函数的实例:

云计算开发:Python内置函数-filter() 函数详解

输出结果 :

[1, 3, 5, 7, 9]

云计算开发:Python内置函数-filter() 函数详解

输出结果 :

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

OpenMagic API

Need more than content? Move into the product flow.

If you are here for model access, pricing, developer docs, or the future API console, the dedicated product path now lives on api.openmagic.ai.