In the recently days, I often analyze the financial data by python. Sometime I feel it spends too long time to wait the result. So I need improve this ability from other people or some articles. I find three functions need to know and use better.
The main reason why I want to cover these functions is that they help you avoid writing loops. Loops can be expensive to run in some cases, and alongside that, these functions will help with speed improvement.
Here are the functions that the article will cover:
map()
filter()
reduce()
Even if you’ve heard of those functions before there’s no harm in reinforcing your knowledge with a bit more theory and examples.
map()
The map() function takes in another function as a parameter, alongside an array of some sort. The idea is to apply a function (the one passed in as an argument) to every item in the array.
filter()
Here’s another one decent function that will save you time — both on writing and on execution. As the name suggests the idea is to keep in array only the items that satisfy a certain condition.
reduce()
Now reduce() is a bit different than the previous two. To start out, we have to import it from the functools module. The main idea behind this is that it will apply a given function to the array of items and will return a single value as a result.
网友评论