端口过滤功能_过滤和减少功能

端口过滤功能

介绍(Introduction)

As discussed in the previous tutorial on Map functions, Filter, and Reduce are typical examples of functional programming. They allow us to write simpler, shorter code, without bothering about loops and branching.

如上一章有关Map函数的教程所述,Filter和Reduce是函数式编程的典型示例。 它们使我们可以编写更简单,更短的代码,而不必担心循环和分支。

Let us see how to use these functions in this tutorial.

让我们看看如何在本教程中使用这些功能。

过滤功能 (Filter Function)

Filter function returns boolean values (which is : true or false) and then passes each element in the iterable through the function, "filtering" away those that are false or we can say that it creates a list of elements for which a function returns true.

过滤器函数返回布尔值(即true或false ),然后将迭代器中的每个元素传递给该函数,“过滤”掉那些不为假的元素,或者可以说它创建了一个元素列表,该函数为此返回true 。

The filter() has the following syntax:

filter()具有以下语法:

filter(function, iterable)

filter(function, iterable)

Only one iterable is required for the filter functions where it passes each element in the iterable through the function and returns only the ones that evaluate to true.

过滤器函数仅需要一个可迭代的函数,该函数将可迭代的每个元素传递给该函数,并返回计算结果为true的那些元素。

Let’s take a look at an example to understand how filter works:

让我们看一个例子来了解过滤器是如何工作的:

Below is a list of the ages of 10 random people.We want to check ages for people who are above the age of 35.

以下是10个随机人群的年龄列表。我们要检查35岁以上人群的年龄。

Image for post

Using lambda with filter function:

使用具有过滤器功能的lambda:

In the below example, we are checking if a word is palindrome or not. A “palindrome” is a word that reads the same forward as backward.

在下面的示例中,我们正在检查一个单词是否是回文字母。 “回文”是一个单词,其前后读相同。

Image for post

Another example :

另一个例子 :

Image for post
Finding numbers greater than Zero in a given range of numbers
在给定的数字范围内查找大于零的数字

减少功能(Reduce Function)

The reduce() function has the following syntax:

reduce()函数具有以下语法:

reduce(function, iterable[,initial])

reduce(function, iterable[,initial])

Where function is the function on which each element in the iterable gets applied to, initial is the (optional) value that gets placed before the elements of the iterable in the calculation, and serves as a default when the iterable is empty. Reduce applies a function of two arguments cumulatively to the elements of an iterable, optionally starting with an initial argument. It is a useful function for performing computation on a list and returning the result.

其中function是应用于iterable中的每个元素的函数,initial是(可选)值,该值在计算中置于iterable的元素之前,并且在iterable为空时用作默认值。 Reduce可以将两个参数的函数累积地应用于可迭代对象的元素,可以选择从初始参数开始。 这对于在列表上执行计算并返回结果非常有用。

Let’s take a look at an example to understand how reduce works:

让我们看一个例子来了解reduce如何工作:

We want to find the sum of list of integers. The traditional way is using a for loop:

我们想要找到整数列表的总和。 传统方式是使用for循环:

Image for post

Now, let’s try with reduce:

现在,让我们尝试减少:

Image for post

In this eg., reduce takes the first and second elements in the list and passes it to the function sum where it computes their sum and returns it to reduce which then takes that result and applies it as the first element to sum and takes the next element (third) in the list as the second element to sum. It does this continuously (cumulatively) until entire list is exhausted.

在这个例子中,reduce获取列表中的第一和第二个元素,并将其传递给函数sum ,在此函数计算它们的总和并返回reduce以获取结果,然后将其用作第一个元素求和并获取下一个列表中的元素(第三个)作为要求和的第二个元素。 它会连续(累积)执行此操作,直到耗尽整个列表。

What if we use the optional initial value:

如果我们使用可选的初始值,该怎么办:

Image for post
Here, reduce uses Here, reduce使用 5(initial value) as the first argument to 5(initial value)作为 sum. sum的第一个参数。

Using lambda with reduce function:

将lambda与reduce函数一起使用:

The same above example(finding sum) using lambda function with reduce.

与上面的示例(求和)相同,使用带有reduce的lambda函数。

Image for post

结论 (Conclusion)

In this tutorial, with the help of few examples we learnt how to use the filter and reduce functions in python.

在本教程中,借助几个示例,我们学习了如何在python中使用过滤器和简化函数。

翻译自: https://medium.com/swlh/filter-and-reduce-functions-ece411b0a5f8

端口过滤功能

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值