Detailed Explanation of Examples of Higher Order Functions map reduce and filter of Python3

  • 2021-07-26 07:58:51
  • OfStack

If the parameters of a function can accept variables, then one function can accept another function as an argument, which is called a high-order function.

Note that: map and filter return 1 lazy sequence, which can iterate objects and need to be converted to list


>>> a = 3.1415
>>> round(a,2)
3.14
>>> a_round = round
>>> a_round(a,2)
3.14
>>> def func_devide(x, y, f):
  return f(x) - f(y)
# Passing parameters as functions 
print(func_devide(9.3, 3.2, round))

1. map function

The map () function takes two arguments, one is a function and one is Iterable. map applies the passed-in function to each element of the sequence in turn and returns the result as the new Iterator.


>>> print(list(map(str, [1, 2, 3])))
['1', '2', '3']
>>> dt = map(str,[-1,2,3,4,5,-34,-45,-23.454])
>>> dt
<map object at 0x10f431dd8>
>>> list(dt)
['-1', '2', '3', '4', '5', '-34', '-45', '-23.454']
>>> dt = map(abs,[-1,2,3,4,5,-34,-45,-23.454])
>>> list(dt)
[1, 2, 3, 4, 5, 34, 45, 23.454]

Note error reporting: TypeError: 'map' object is not callable

Iterative objects (str, abs, etc.) or functions (map) are modified and are no longer the original functions, resulting in non-iterative objects

2. reduce function

reduce applies a function to a sequence [x1, x2, x3,...], which must take two parameters, and reduce continues the result and cumulates it with the next element of the sequence. Returns the final result of one calculation, and the function accepts two parameters:


def add(x,y):
...   return x + y
... 
>>> reduce(add,[1,2,3,4,5,6,7,8,9,10])
55
>>> def concate(x,y):
...   return str(x)+str(y)
... 
>>> reduce(concate,[1,2,3,4,5,6,7,8,9,0])
'1234567890'

The combination of reduce and map functions for string-to-integer (or integer-to-string)


>>> str = '12121212132323'
>>> dic_str_int = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
>>> def str_arr(x):
...   dic_str_int = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
...   return dic_str_int[x]
... 
>>> def int_dum(x,y):
...   return 10*x + y
... 
>>> reduce(int_dum,map(str_arr,str))
12121212132323

Example, convert the data in the list to uppercase, and the first letter is uppercase


>>> names = ['jack','john','wilianmon','jobs','bill','gates']
>>> def str_upper(string):
...   return string.upper()
... 
>>> names = map(str_upper,names)
>>> list(names)
['JACK', 'JOHN', 'WILIANMON', 'JOBS', 'BILL', 'GATES']
>>> def str_capitialize(string):
...   return string.capitalize()
... 
>>> names = ['jack','john','wilianmon','jobs','bill','gates']
>>> 
>>> names = map(str_capitialize,names)
>>> list(names)
['Jack', 'John', 'Wilianmon', 'Jobs', 'Bill', 'Gates']

The parameters in the list multiply all elements:


int_li = [2,3,5,10]
>>> reduce(lambda x, y: x*y,int_li)
300
>>> def func_mult(li=None):
...   return reduce(lambda x, y: x*y,li)
... 
>>> func_mult(int_li)
300

The above can be converted into functions according to needs, which is more convenient to call

Convert '123.456' to an integer of 123.456

Method 1: Splice after truncation


def string_int(strs):
  str_li = strs.split('.')
  def str_int(str):
    dic_str_int = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
    return dic_str_int[str]
  int_1 = reduce(lambda x, y: x*10+y, list( map(str_int,str_li[0])))
  int_2 = reduce(lambda x,y: x*10 + y,list(map(str_int,str_li[1])))
  return int_1 + int_2/(10**(len(str_li)+1))

res = string_int('123.456')
print(res)
# Results: 123.456

Method 2: Convert to a pure numeric string


def string_int1(strs):
  #  Remember the location, replace 
  point_len = len(strs) - strs.find('.')-1
  str_li = strs.replace('.', '')
  def str_int(str):
    dic_str_int = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
    return dic_str_int[str]
  int_num = reduce(lambda x,y: x*10 + y,list(map(str_int,str_li)))
  return int_num/(10**(point_len))

res = string_int1('123.456')
print(res)
# Results: 123.456

3. filter function

filter () also receives a function and a sequence. Screen out the eligible elements from 1 sequence. Unlike map (), filter () applies the passed-in function to each element in turn, and then decides whether to keep or discard the element depending on whether the return value is True or False.

Note: Difference from map function

函数名 区别
map 作用于每个可迭代对象的元素,并返回处理之后的元素
filter 作用于可迭代内每个元素,根据计算后结果:True保留,Flase去掉

eg: Get all integer type elements in the list


def only_int(x):
  try:
    if isinstance(x, int):
      return True
    else:
      return False
  except ValueError as e:
    return False
dt = filter(type_int,[1,2,3,3,'3232',-34.5,34.5])
>>> list(dt)
[1, 2, 3, 3]

Summarize


Related articles: