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

news/2024/7/10 2:43:23 标签: python, 数据库, vue, linux, php

端口过滤功能

介绍(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

端口过滤功能


http://www.niftyadmin.cn/n/952925.html

相关文章

python小鱼儿视频_八皇后问题的python实现

最近在学python,自己看了点书,对照例子编程,其中讲迭代的一章提到了八皇后问题,也就是在一个国际象棋棋盘上,放8个皇后,并且皇后之间彼此不能形成威胁。小时候玩过国际象棋,知道皇后除了不能走马…

react 状态管理工具_React查询低估状态管理工具

react 状态管理工具React is one of the most loved frontend libraries in the developer community. Together with React, the terms like Virtual DOM, Functional Components, State Management, and Higher-Order Components have emerged. Among these terms, State Man…

write函数_python中write和writelines的区别有哪些?

之前小编向大家介绍了python用write函数写入文件方法(),它在python中其到的是输入的作用。在python,还有一个写入文件的方法,那就是和它长得很像的writeline函数。这两者具体如何使用,有什么区别吗?本文小编从参数、格…

状态变量表示

Before diving into the details, we need to understand how we represent the data.在深入研究细节之前,我们需要了解如何表示数据。 If you haven’t read the previous post, the introduction, here’s the link.如果您还没有阅读上一篇文章的介绍,…

使用函数求奇数和_VOL6. Lambda函数的用法分析

lambda函数在最新的PEP(python编码规范)中已经指出不建议使用了,但是因其简介性还是有一定使用价值,今天就来讨论一下lambda函数的用法。一、简介lambda函数又称匿名函数,很好理解,就是没有名字的函数。与之…

python jdict_jdict python中的javascript dict

python jdict致谢(Acknowledgment) Thanks to Asher Sterkin, BlackSwan Technologies SVP of Engineering, who proposed the idea for this project and provided invaluable guidance on its development. The project is an offshoot of BlackSwan’s work developing a Cl…

python中槽是什么_【Python】format()中槽的嵌套以及range()函数用法

星号三角形读入一个整数N,N是奇数,输出由星号字符组成的等边三角形,要求:‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬…

手机界面设计模式_流利的界面设计模式

手机界面设计模式C#概念(C# CONCEPTS) 什么是方法链接?(What is Method Chaining?) It’s used to link multiple methods calls into a single compound statement. Method Chaining requires that every method return an interface that supports ch…