In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what are the Python functions of reducing loops?". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Map ()
The map () function takes another function as an argument, along with some array, and aims to apply the function (the function imported as an argument) to each item in the array. The benefits are as follows:
No need to write a loop
It takes less time than writing a loop
Let's see the actual effect. The author uses a function called num_func (), which takes a number as an argument. Square the number and divide it by two, and then return.
Note: the operation of the function is optional, and the user can perform any operation you want in the function.
Now declare an array of numbers to which you want to apply num_func (). Note: map () itself will return a map object, so you need to convert it to a column:
The great task has been completed! It's not a difficult technology, but it's always good to avoid loops as much as possible.
Reduce ()
Unlike reduce (), it must be imported from the functools module. The main purpose of this operation is to apply a given function to the project group and return a value.
The last part is critical-the reduce () function does not return a group of projects, but a value. Observe the following chart to materialize this concept.
When the chart is not completely clear, the following logic is presented:
1.5 plus 10 equals 15
2.15 plus 12 equals 27
3.27 plus 18 equals 45
4.45 plus 25 equals 70
70 is the returned value. Starting with the implementation code, import the reduce function from the functools module, and then declare a function that returns the sum of two numbers:
You can now re-view the chart in the code and verify that everything is working properly:
Of course, there are many ways to summarize the items in the list, and here are only some of the simplest examples of how functions work.
Filter ()
This function is also great, and it saves time-- including writing and execution. As the name implies, the purpose of this function is to keep only items that meet certain conditions in the array.
As with map (), the function is predeclared and then passed to filter () along with the iteration list.
An example: a function named more_than_15 () has been declared in advance, that is, if the item given by the parameter is greater than 15, True is returned.
Next, declare an array of numbers and import it as the second parameter in the filter () function:
This is the end of the content of "what are the Python functions for reducing loops"? thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.