Home  >  Article  >  Backend Development  >  A brief introduction to lambda expressions in Python (with examples)

A brief introduction to lambda expressions in Python (with examples)

不言
不言forward
2018-10-10 15:34:013063browse

This article brings you a brief introduction to lambda expressions in Python (with examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1: Definition of anonymous function

lambda parameter_list: expression

2: Ternary expression

The result returned when the condition is true if conditional judgment Else The result returned when the condition is false

3: map

map(func(arg1, arg2...), list1_arg1, list2_arg2), execute the previous one on the list input later. Function (mathematical mapping)

4: reduce

reduce(func(arg1, arg2...), list1_arg, init_value), continuous calculation, continuous call of lambda expression

五:filter

filter(func(arg1, arg2...), list1_arg1) When the conditions are met, the data will be filtered out!

6: Functional programming and imperative programming

def
    if --else
    for
    map reduce filter
    lambda

The idea of ​​functional programming. . . .
The idea of ​​imperative programming. . . .

Functional programming cares about the mapping of data, and imperative programming cares about the steps to solve problems

Functional programming:

(1) refers to functions and other The data types are the same and are on an equal footing. They can be assigned to other variables, can also be used as parameters, passed into another function, or used as the return value of other functions.

(2) Only use "expression" instead of "statement"

from functools import reduce
# ----------------------------------------------------------------#
#   匿名函数的定义
# ----------------------------------------------------------------#


def add(x, y):
    """
    add x and y
    :param x: x can be str or num
    :param y: y can be str or num
    :return:  x+y
    """
    return x + y


# lambda parameter_list: expression
user_sum = lambda arg1, arg2: arg1 + arg2

my_sum = user_sum(2, 2)
print(my_sum)

# ----------------------------------------------------------------#
#   三元表达式
# ----------------------------------------------------------------#
a, b = 1, 2
r = a if a > b else b
print(r)

# ----------------------------------------------------------------#
#   map(func, list),对后面输入的list分别执行前面的函数(数学的映射)
# ----------------------------------------------------------------#

myListMap1 = [1, 2, 3, 4]
myNewListMap1 = map(lambda x: x ** 2, myListMap1)  # 返回为map类型的数据结构
print(type(myNewListMap1))
print('myNewListMap1:', list(myNewListMap1))    # 转换为list


# 两个或者多个参数的map函数的使用
# 当两个参数种元素的个数不相同的时候会截断
myListMap2 = [1, 2, 3, 4]
myNewListMap2 = map(lambda x, y: x + y, myListMap1, myListMap2)
print('myNewListMap2:', list(myNewListMap2))


# ----------------------------------------------------------------#
#   reduce(func, list)连续计算,连续调用lambda表达式
# ----------------------------------------------------------------#

myListReduce = [1, 2, 3, 4]
# 把list中的值一个一个放进lambda中
r = reduce(lambda x, y: x + y, myListReduce)
print(r)

# 对第一个函数参数进行初始化
r = reduce(lambda x, y: x + y, myListReduce, 10)
print(r)

# filter
myListFilter = [3, 5, 6, 7, 8, 9, 10]
myNewListFilter = filter(lambda x: x % 2 == 1, myListFilter)
print('myNewListFilter:', list(myNewListFilter))

list_x = [1, 1, 0, 0]
filter_list = filter(lambda x: True if x == 1 else False, list_x)
print(list(filter_list))

The above is the detailed content of A brief introduction to lambda expressions in Python (with examples). For more information, please follow other related articles on the PHP Chinese website!

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