python higher-order functions map & filter & reduce & sorted

Higher-order functions to the function as a parameter of the function

 

Reference links

  Higher-order functions

 

surroundings

  python3.6

 

map

  mapFunction takes two parameters, a function, a is the sequence, mapthe function is passed sequentially applied to each element of a sequence corresponding to the index, as a final result generator returns. Parameters can be passed more sequences of sequences

  1. When a plurality of sequences, if the sequence length of the shortest of all the elements have been mapped, the end of the entire map function

from collections import Iterator


a_li, b_li, c_li = list(range(10)), list(range(104)), list(range(1, 15))
m1 = map(lambda x, y, z: x + y + z, a_li, b_li, c_li)
print(isinstance(m1, Iterator))
print(list(m1))

  Returned to the generator iteration only elements of a sequence of the shortest of all, the result is

True
[1, 4, 7, 10, 13, 16, 19, 22, 25, 28]

  2. In addition to using anonymous function function parameters, you can also use a custom mapping function 

def map_func(x, y, z):
    return x + y + z + 10


m2 = map(map_func, a_li, b_li, c_li)
print(list(m2))

  Passed into the function name, the result is

  [11, 14, 17, 20, 23, 26, 29, 32, 35, 38]

 

reduce

  reduce to a function on a sequence [x1, x2, x3 ...], the function must receive two parameters, and continue to reduce the result of the next element in the sequence do cumulative basis, the effect is

reduce(f, [x1, x2, x3, x4]) = f(f(f(x1, x2), x3), x4)

  1. The numeric string to integer

s = "12344"
num_dic = {num: int(num) for num in "0123456789"}
res = reduce(lambda x, y: x * 10 + y, map(lambda i: num_dic[i], s))
print(res)

  map a string into digital sequence generator and returns, redcue sequence according to Integer anonymous function, the result is

12344

  The first parameter to reduce the initial parameter initialization anonymous functions, initial = 9

res2 = reduce(lambda x, y: x * 10 + y, map(lambda i: num_dic[i], s), 9)
print(res2)

  Results

912344

  

filter

  filter()Function is used to filter sequences. filter()And a function of receiving a sequence, the function is passed successively to each element, based on the returned value Trueis Falsedecided to keep or discard the element, and finally returns the generator object

  1. Filter b_li sequence, returns the square root of an integer generator

import math


f2 = filter(lambda x: math.sqrt(x) % 1 == 0, list(range(100)))
print(list(f2))

  The square root of the division for obtaining a remainder, the result is

[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

 

sorted

  Sorting is comparing two elements. Digital direct comparison, according to ascii string comparison, dict available items () comparison of the health, the value of other elements. The key parameter can implement custom ordering, i.e., the key is to achieve a sorted mapping function

  1. achieved in descending order of numbers, reversse ascending parameter defaults to False.

order_li = sorted([36, 5, -12, 9, -21], reverse=True)
print(order_li)

  Results

[36, 9, 5, -12, -21]

  2. The numbers in ascending order to achieve absolute

order_li = sorted([36, 5, -12, 9, -21], key=lambda x: abs(x))
print(order_li)

  Results

[5, 9, -12, -21, 36]

  3. ignore case, ascending order to achieve the first letter

order_li = sorted(['bob', 'Zoo', 'Credit'], key=lambda x: x.lower())
print(order_li)

  Into all lowercase, according to alphabetical order, the result is

['bob', 'Credit', 'Zoo']

  4. dictionary sort. According to health ascending order

dic = {2: [30, 'y'], 1: [96, 'i'], 3: [21, 'w'], 9: [40, 'y'], 7: [20, 'y']}
print(dic.items())

order_dic_li = sorted(dic.items(), key=lambda x: x[0])
order_dic = {i[0]: i[1] for i in order_dic_li}
print(order_dic)

  Gets a dictionary items () method, the object is dict_items. Sort Sort i.e. Kin The first element dict_items object, the result is

dict_items([(2, [30, 'y']), (1, [96, 'i']), (3, [21, 'w']), (9, [40, 'y']), (7, [20, 'y'])])
{1: [96, 'i'], 2: [30, 'y'], 3: [21, 'w'], 7: [20, 'y'], 9: [40, 'y']}

  5. dictionary sort. Sorting according to element value, the priority value of the second element, the first element, both are in ascending order

order_dic_li = sorted(dic.items(), key=lambda x: (x[1][1], x[1][0]))
order_dic = {i[0]: i[1] for i in order_dic_li}
print(order_dic)

  Two element values ​​sorted, the result is

{1: [96, 'i'], 3: [21, 'w'], 7: [20, 'y'], 2: [30, 'y'], 9: [40, 'y']}

  6. value for the second element, the first element of the element according to the value of the dictionary ordering priority; in ascending order of the second element, the first element DESC

order_dic_li = sorted(dic.items(), key=lambda x: (x[1][1], -x[1][0]))
order_dic = {i[0]: i[1] for i in order_dic_li}
print(order_dic)

  Results

{1: [96, 'i'], 3: [21, 'w'], 9: [40, 'y'], 2: [30, 'y'], 7: [20, 'y']}

 

Guess you like

Origin www.cnblogs.com/yuanxuetao/p/11357567.html