Home  >  Article  >  Backend Development  >  Python Programming: Excellent Ways to Avoid Loops!

Python Programming: Excellent Ways to Avoid Loops!

PHPz
PHPzforward
2023-04-26 15:10:081718browse

We know that using loops in Python is very slow. What should you do if you are dealing with a similar situation?

不要在 Python 中使用循环,这些方法其实更棒!

In this article, I will share with you methods and examples that can be used to replace Python loops:

  • Map
  • Filter
  • Reduce

#Before you start using the above functions, if you are not familiar with lambda functions, let’s take a quick look.

Lambda functions are an alternative to regular functions. It can be defined in one line of code and therefore takes up less time and space in our code. For example, in the code below, we can see the lambda function in action.

def multiply_by_2(x):
x*2

lambda function

lambda x: x*2

Note: It is better to use lambda function instead of regular function.

1. Map

Using the map function, we can apply the function to each value of the iterable object (list, tuple, etc.).

map(function, iterable)

Suppose we want to get a square number in a list (iterable object). We will first create a square() function to find the square of a number.

def square(x):
return x*x

We will then use the map function to apply the square() function to the input list of numbers.

input_list = [2, 3, 4, 5, 6]
# Without lambda 
result = map(square, input_list)
# Using lambda function 
result = map(lambda x: x*x, input_list)
# converting the numbers into a list
list(result)
# Output: [4, 9, 16, 25, 36]

2. Filter

Intuitively, the filter function is used to filter out values ​​from iterable objects (lists, tuples, sets, etc.). Filter conditions are set within the function passed as a parameter to the filter function.

filter(function, iterable)

We will use the filter function to filter values ​​less than 10.

def less_than_10(x):
if x < 10:
return x

We will then use the Filter function to apply the less_than_10() function to the list of values.

input_list = [2, 3, 4, 5, 10, 12, 14] 
# Without lambda
list(filter(less_than_10, input_list))
# using lambda function 
list(filter(lambda x: x < 10, input_list))
# Output: [2, 3, 4, 5]

3. Reduce

The Reduce function is a little different from the map and filter functions. It is applied iteratively to all values ​​of the iterable object and returns only one value.

In the following example, a list of numbers is reduced by applying the addition function. The final output will be the sum of all the numbers in the list, which is 15. 不要在 Python 中使用循环,这些方法其实更棒!Let's create an addition() function that adds two input numbers.

def addition(x,y):
return x + y

Next, in order to get the sum of all the numbers in the list, we will apply this addition function as an argument to the reduce function.

from functools import reduce
input_list = [1, 2, 3, 4, 5]
# Without Lambda function
reduce(addition, input_list))
# With Lambda function
reduce(lambda x,y: x+y, input_list))
# Output: 15


The above is the detailed content of Python Programming: Excellent Ways to Avoid Loops!. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:51cto.com. If there is any infringement, please contact admin@php.cn delete