The python map() function implements the same processing for each element in the list (passing multiple parameters) (efficient operation)

This time you need to add the same string to all elements in a list (such as ['1', '2', '3', ...] --> ['s1', 's2', 's3', ... ]), it can be implemented with a for loop, but it doesn’t feel so elegant. I guess python must have a more efficient way to implement this operation. Exploration found that although there is no direct function or syntax implementation, it can be implemented indirectly through the map() function, and the efficiency is much higher.
The map function was rarely used before this.


grammar

The map() function is python's built-in function
Syntax: map(function, iterable, …)
Parameters: function - function; iterable - sequence
Returns: iterator. The returned result is a map object, which can be converted to a list type using list(), but attention should be paidThe list generated after converting to a list is a new list (the original list is not changed), and needs to be assigned to a new list variable!

Grammar reference: https://www.runoob.com/python/python-func-map.html

basic example

test_list = ['1', '2', '3']


def y(x):	# 函数中可以对x进行任意操作,然后返回一个值
    return 's' + x


list2 = list(map(y, test_list))
print(list2)
# 输出结果为:['s1', 's2', 's3']

'''
# 用for循环实现如下:
test_list = ['1', '2', '3']
list3 = []
for i in test_list:
    list3.append('s' + i)
print(list3)
'''

The function can xperform any operation on the object and return a value. However map(y, test_list), yit is not possible to pass parameters to the function (personal guess), such as defining a function def y(x, str):, but it cannot be used map(y(str), test_list). Therefore, two other methods were found to achieve the effect of passing multiple parameters. See below for details.

For more basic usage examples, please refer to:
https://blog.csdn.net/quanlingtu1272/article/details/95482253

Example of passing multiple parameters

Method 1. Use lambda expressions (recommended)

Lambda expressions are also called anonymous functions, which are exactly the same as ordinary functions, but there is no function name defined by def, which is suitable for use only once in the program.
The following example is equivalent to passing the variable bsum ainto the lambda function.

test_list = ['1', '2', '3']
a = 'v'
b = 's'

list2 = list(map(lambda x: a + b, test_list))

print(list2)
# 输出结果为:['vs1', 'vs2', 'vs3']

Lambda expression can refer to: https://blog.csdn.net/answer3lin/article/details/86352009
map()+lambda reference: https://blog.csdn.net/qq_44214671/article/details/110144525

Method 2. Create a list of equal length

Create a list of equal length and fill it with the same elements, and add the elements in their corresponding positions.

test_list = ['1', '2', '3']


def y(a, b, c):
    return a + b + c


a = ['v'] * len(test_list)
b = ['s'] * len(test_list)
list2 = list(map(y, a, b, test_list))
print(list2)
# 输出结果为:['vs1', 'vs2', 'vs3']

For adding the corresponding position elements of two equal-length lists, please refer to:
https://blog.csdn.net/weixin_42782150/article/details/124746314
For creating a list and filling the same elements, please refer to:
https://blog.csdn. net/sunxmwebstudy/article/details/111414278
What happens to the running results if the lists are not equal in length? Reference ("Example Code 4"):
https://blog.csdn.net/weixin_44799217/article/details/119799110

conclusion

Why use the map function compared to the for loop
? Of course, the map is more efficient. If someone has done an experiment, you can share the efficiency comparison results in the comment area.

In the "basic example" the function ycan't pass parameters, so can't it define a global variable? sure! But in some cases it is inconvenient.

I have learned so much, if there is something wrong, you can point it out, thank you very much

Guess you like

Origin blog.csdn.net/qq_41999731/article/details/125774017