Detailed explanation of python's map, zip and filter iterator examples

In Python, the three functions map, zip, and filter are very commonly used iterator functions, which can easily operate on sequences. This article will introduce the three functions of map, zip and filter, their usage methods and examples, and help you better understand and apply these three functions.

1.
map The map function is a built-in high-order function in Python. Its function is to apply a function to each element in the iterator and return a new iterator.

The basic syntax of the map function is as follows:

map(function, iterables)

Among them, function is the function used to process each element in the sequence, and iterables is one or more sequences (lists, tuples, strings, etc.), which will be passed into the function function one by one.

The map function returns a new iterator containing the processed results. Use the list function to directly convert an iterator into a list.

The following example will square each element in the list:

lst = [1, 2, 3, 4, 5]
result = map(lambda x: x**2, lst)
print(list(result))

The output is: [1, 4, 9, 16, 25]

In addition, since the map function returns an iterator, there is no need to store the sequence in memory in advance, and elements can be processed one by one, saving memory space.

2.zip
The zip function is also a built-in high-order function of Python. Its function is to map the elements in multiple iterators into a tuple one by one and return a new iterator. The zip function will intercept according to the length of all input iterable objects, and only intercept the length of the shortest iterator.

The basic syntax of the zip function is as follows:

zip(*iterators)

Among them, an iterator is one or more iterable objects (most commonly sequences).

The zip function returns a new iterator that contains tuples that correspond to the elements in multiple iterators. Use the list function to directly convert an iterator into a list.

The following example will form a tuple for the elements in the two lists:

lst1 = [1, 2, 3, 4, 5]
lst2 = ['a', 'b', 'c', 'd', 'e']
result = zip(lst1, lst2)
print(list(result))

The output is: [(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e')]

In addition, the return value of the zip function can be stored in multiple variables by decompression:

lst1 = [1, 2, 3, 4, 5]
lst2 = ['a', 'b', 'c', 'd', 'e']
result = zip(lst1, lst2)
new_lst1, new_lst2 = zip(*result)
print(new_lst1)  # (1, 2, 3, 4, 5)
print(new_lst2)  # ('a', 'b', 'c', 'd', 'e')

3. filter
The filter function is also a built-in high-order function of Python, its role is to filter elements that meet the conditions and return a new iterator.

The basic syntax of the filter function is as follows:

filter(function, iterables)

Among them, function is a function whose return value is Boolean (True or False), which is used to filter the required elements. iterables is a sequence that contains elements that need to be filtered.

The filter function returns a new iterator containing only elements that satisfy the condition. Use the list function to directly convert an iterator into a list.

The following example will filter out elements greater than 3 from a list:

lst = [1, 2, 3, 4, 5]
result = filter(lambda x: x > 3, lst)
print(list(result))

The output is: [4, 5]

To sum up, the three iterators of map, zip and filter are widely used in Python, and they can easily operate on sequences. In practical programming, they are often used in areas such as data processing, statistical analysis, and scientific computing.

In addition to the above general usage, these three functions can also be used in some special scenarios, the following are some common usages:

4. The map function is applied to multiple sequences
The map function can be applied not only to one sequence, but also to multiple sequences. In this case, the lambda function needs to have multiple parameters, corresponding to all sequences. For example:

lst1 = [1, 2, 3, 4, 5]
lst2 = [10, 20, 30, 40, 50]
result = map(lambda x, y: x + y, lst1, lst2)
print(list(result))

The output is: [11, 22, 33, 44, 55]

5. The zip function is applied to decompression
In addition to the one-to-one correspondence of the elements in multiple iterators into a tuple, the zip function can also be used for decompression. In this case, the argument to the zip function needs to use the * operator to decompress a tuple or list into multiple variables. For example:

lst = [(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e')]
new_lst1, new_lst2 = zip(*lst)
print(new_lst1)  # (1, 2, 3, 4, 5)
print(new_lst2)  # ('a', 'b', 'c', 'd', 'e')

6. The filter function is applied to deduplication
The filter function can also be applied to deduplication. In this case, the function function needs to return a Boolean value indicating whether the list element has already appeared, and filter out the elements that appear for the first time one by one. For example:

lst = [1, 3, 2, 4, 1, 3, 5, 6, 5, 4, 6]
result = filter(lambda x: lst.index(x) == lst.index(list(filter(lambda y: y == x, lst))[0]), lst)
print(list(result))

The output is: [1, 3, 2, 4, 5, 6]

In short, the three functions map, zip and filter are very commonly used iterator functions in Python. They can easily process various types of sequences, simplify code writing and improve efficiency. Mastering the usage methods and skills of these three functions can help us better apply Python for programming.

おすすめ

転載: blog.csdn.net/naer_chongya/article/details/131211977
おすすめ