Eraser, a new series of fun Internet advanced webworms, let’s Be More Pythonic together.

Filter, Map, Reduce, ZIP, enumerate

These functions are called higher-order functions in Python, and this article will focus on their usage.

6.1 the filter

The filter function prototype is as follows:

filter(function or None, iterable) --> filter object
Copy the code

The first argument is a judgment function(which returns True or False), and the second is a sequence that performs function(item) on the iterable sequence, which returns a filtered sequence of results. Simple memory: Filter the elements in the sequence to obtain the sequence that meets the conditions.

my_list = [1.2.3]
my_new_list = filter(lambda x: x > 2, my_list)
print(my_new_list)
Copy the code


is returned. The list function can be used to input the sequence content.

6.2 the map

The map function prototype is as follows:

map(func, *iterables) --> map object
Copy the code

After this function is run, a list is generated with the function as the first argument and one or more sequences as the second argument. The following code is a simple test case:

my_list = [-1.2, -3]
my_new_list = map(abs,my_list)
print(my_new_list)
Copy the code

Print (list(my_new_list)) to get the result.

The first argument to a map function can have multiple arguments, and when this happens, the second argument after it needs to be multiple sequences.

def fun(x, y) :
    return x+y
The # fun function takes two arguments, so two sequences are required
my_new_list = map(fun, [1.2.3], [4.4.4])
print(my_new_list)
print(list(my_new_list))
Copy the code

The map function solves the following problems:

  1. usemapFunction, without creating an empty list;
  2. When you call a function, you don’t have to have parentheses,mapThe function automatically calls the target function;
  3. mapThe function automatically matches all elements in the sequence.

6.3 the reduce

Reduce function prototype is as follows:

reduce(function, sequence[, initial]) -> value
Copy the code

The first argument is a function, and the second argument is a sequence that returns the computed value. The value of this function is to scroll through successive values applied to a list. The test code is as follows:

from functools import reduce
my_list = [1.2.3]

def add(x, y) :
    return x+y

my_new_list = reduce(add, my_list)
print(my_list)
print(my_new_list)
Copy the code

The final result is 6. If you set the third parameter to 4, you can run the code to see the result. Finally, you can conclude that the third parameter represents the initial value, the initial value of the summation operation.

my_new_list = reduce(add, my_list,4)
print(my_list)
print(my_new_list)
Copy the code

Simple memory: Cumulative operations on all elements in a sequence.

6.4 the zip

The zip function prototype is as follows:

zip(iter1 [,iter2 [...]]) --> zip object
Copy the code

The zip function takes an iterable object as an argument, packs the corresponding elements of the object into tuples, and returns a list of those tuples. If each iterator has a different number of elements, it returns a list of the same length as the shortest object. Using the asterisk (*) operator, the tuple can be unpacked into a list. The test code is as follows:

my_list1 = [1.2.3]
my_list2 = ["a"."b"."c"]
print(zip(my_list1,my_list2))
print(list(zip(my_list1,my_list2)))
Copy the code

Shows how to use the * operator:

my_list = [(1.'a'), (2.'b'), (3.'c')]
print(zip(*my_list))
print(list(zip(*my_list)))
Copy the code

The following output is displayed:

<zip object at 0x0000000002844788>
[(1, 2, 3), ('a'.'b'.'c')]
Copy the code

Simple memory: Zip maps similar indexes to multiple containers, which can be easily used to construct dictionaries.

6.5 enumerate

The prototype of the enumerate function is as follows:

enumerate(iterable, start=0)
Copy the code

Parameter Description:

  • sequence: a sequence, iterator, or other object that supports iteration;
  • start: starting subscript position.

This function is used to combine a traversable data object into a sequence of indexes, listing both the data and the data subscripts, commonly used in a for loop. The test code is as follows:

weekdays = ['Mon'.'Tus'.'Wen'.'Thir']
print(enumerate(weekdays))
print(list(enumerate(weekdays)))
Copy the code

< Enumerate object at 0x0000000002803AB0> is displayed.

6.6 Summary of this blog

The functions involved in this paper can be combined with lambda expressions to greatly improve the coding efficiency. The best learning materials are always the official manuals


Today is the 102/200th day of continuous writing.

Blogger ID: Dream eraser, hope you like, comment, favorites.