[Python basics] Use of the four basic functions of map, reduce, lambda, and filter

map function

The map function maps the specified sequence (iterable) according to the provided function

定义:map(function, sequence[,sequence,…])—>list

Returns a map type of iterable objects

def create_content(mes):
    return map(lambda x: x+2, mes)


def write_content_item(result):
    for i in result:
        print(i, end=" ")


content = create_content([1, 2, 3])

# 返回一个map对象
print(type(content))
# map对象具有__iter__和__next__方法,属于可迭代对象
print(content.__next__())

write_content_item(content)

content = create_content((1, 2, 3))

write_content_item(content)
'''
    map第二个参数开始必须输入的是可迭代对象
'''
try:
    content = create_content(1)
    write_content_item(content)
except Exception as e:
    print("输入了非迭代对象")
print("第二次实验------多输入参数")
content = map(lambda x, y: x + y, {
    
    1, 2}, {
    
    3, 4})
for i in content:
    print(i)

content = map(lambda x, y, z: x + y - z, {
    
    1, 2}, {
    
    3, 4}, {
    
    -1, 3})
for i in content:
    print(i)

It can be seen from the above:

  1. The input sequence[,sequence,…] must be an iterable object
  2. The number of parameters of the previous function call needs to be consistent with the number of iteration objects entered later

filter function

The filter function filters the specified sequence

定义:filter(function or None, sequence) -> list, tuple, or string

print(list(filter(lambda x: 'a' <= x <= 'z', '132a')))

reduce function

The reduce function will accumulate the elements in the parameter sequence
(the structure of the last call will be passed to the next call)

定义:reduce(function, sequence[, initial]) -> value

Note: function must have two parameters function

from functools import reduce

def get_all(content):
    return reduce(lambda x, y: 10*x + y, content)

print(get_all([1, 2, 3, 4, 5]))

reduce(functiion(x,y),[p1, p2, p3, p4])<=>f(f(f(p1,p2),p3),p4)

lambda function

When passing in a function, sometimes it is not necessary to explicitly define the function, it is more convenient to pass in an anonymous function directly

# lambda构建匿名函数
f = lambda x, y: x + y

print(f(1, 2))

There are advantages to using anonymous functions:

  1. Because the function has no name, don’t worry about the function name
  2. . In addition, an anonymous function is also a function object. You can also assign an anonymous function to a variable, and then use the variable to call the function:
>>> f = lambda x: x * x
>>> f
<function <lambda> at 0x101c6ef28>
>>> f(5)
25
  1. Similarly, you can also return anonymous functions as return values
def build(x, y):
    return lambda: x * x + y * y

Finally, let's make a small case

Extract integers in a string

Sample input
123asdasdfgw123
Sample output
123123

from functools import reduce


# 1、输入一个字符串
mes = input("请输入你要转化为整数的字符串>>")
# 2、筛选出其中的数字字符
str_number = filter(lambda x: '0' <= x <= '9', mes)
# 3、将数字字符串转化为数字迭代器
number_iterator = map(lambda x: int(x), list(str_number))
# 4、将数字迭代器转化为一个整数
number = reduce(lambda x, y: 10*x + y, list(number_iterator))

print(number)

Guess you like

Origin blog.csdn.net/hide_in_darkness/article/details/109227183