Lecture 43: Python uses map and filter functions to traverse iterable objects

1. The difference between traversal iteration and map and filter functions

Both functions map and filter can traverse the specified iterable object (list, string, tuple, dictionary, collection, etc.) according to another function specified, and then produce a new iterable object.

The core function is to process all the elements in the iterable object according to a specified function, and then return a new iterable object.

The usage of the two functions is the same, and both need to specify two parameters, the first parameter is the specified function name, and the second parameter is the specified iterable object.

map(函数, 可迭代对象)

filter(函数, 可迭代对象)

Both the map function and the filter function process all elements in the iterable object according to a function, so what is the difference between the two?

  • After calling the built-in function map, the specified function will be used to traverse each element in the specified iterable object, and then a new iterable object will be generated.
  • After calling the built-in function filter, the specified function will be used to traverse each element in the specified iterable object, and then filter out the elements whose function processing result is False to generate a new iterable object.

2. Use the map function to traverse iterable objects

The ord function we learned earlier can only return its corresponding ordinal value to one character at a time. After we learn the map function, we can use the map function to query the corresponding ordinal value of multiple characters at the same time using the ord function.

The map function mainly uses the specified function to process all the elements in the iterable object according to its traversal characteristics.

print(map(ord, 'abcde67'))
#map函数返回值是一个迭代器对象  <map object at 0x0000023D0BD8E650>

#可以使用list将其转换成列表格式
print(list(map(ord, 'abcde67')))
#输出结果:[97, 98, 99, 100, 101, 54, 55]

The result is as follows, the map function processes all the character elements in the specified iterable object string according to the ord function we specified, and then uses the list function to convert it into a list.

image-20220815220115591

The map function actually does this when traversing: ord(a), ord(b), ord©, each time traversing uses the specified function to process the elements in the iterable object.

Use the upper function to process each character in the string and convert it to an uppercase letter.

print(list(map(str.upper, 'abcde')))

image-20220815220403943

3. Use the filter function to traverse the iterable object

After calling the built-in function filter, the specified function will be used to traverse each element in the specified iterable object, and then filter out the elements whose function processing result is False to generate a new iterable object.

When the filter function traverses, after the specified function processes the elements in the iterable object, the return value is True, it will be listed, and the return value is False, it will be filtered out.

We can specify the method str.isalpha, and use the map and filter functions to process the elements in the iterable object. When the elements are all letters, it will return True, otherwise it will be False. The filter function will filter out the elements whose return value is False .

The same function, the same iterator object, what is the difference when we observe map and filter traversal.

print(list(map(str.isalpha, ['jiangxl', 'python', '1', '2', '3'])))

print(list(filter(str.isalpha, ['jiangxl', 'python', '1', '2', '3'])))

The map function will print out whether the specified function and element are True or False after processing, and the filter function will only return elements with a value of True.

image-20220815221059647

Guess you like

Origin blog.csdn.net/weixin_44953658/article/details/130635192