Article directory
1. Introduction to map() function
When we use lists or tuples, we often need to perform mathematical transformation operations on the list elements and return a new list. For example, we want to multiply each element in the list bonuses by 2:
bonuses = [100, 200, 300]
To do this, we can use a for loop to iterate through the list, multiply each element by 2, and add the result to a new list:
bonuses = [100, 200, 300]
new_bonuses = []
for x in bonuses:
new_bonuses.append(x*2)
print(new_bonuses) # 输出:[200, 400, 600]
1.1 Basic syntax of map() function
Python provides a built-in map() function that makes it easier to perform such operations.
Following is the basic syntax of map() function −
map(function, iterable)
The parameters are explained as follows:
- function: a function or method
- iterable: one or more sequences (iterable objects)
The function of the map() function is to call the function function on each element in the sequence iterable and return a map object instance. This map object is essentially an iterator.
Returning to the example above, we can define a function that multiplies by 2 and then call that function using the map() function:
def double_func(x):
return x* 2
bonuses = [100, 200, 300]
iterator = map(double_func, bonuses)
1.2 map() function + lambda expression
Or you can lambda 表达式
further simplify the code, for example:
bonuses = [100, 200, 300]
iterator = map(lambda x: x*2, bonuses)
print(list(iterator)) # 输出:[200, 400, 600]
1.3 The map() function inputs multiple iterable objects iterable
b1 = [100, 200, 300]
b2 = [1, 2, 3]
iterator = map(lambda x,y : x*y, b1, b2)
print(list(iterator)) # 输出:[100, 400, 900]
1.4 View the contents of the returned iterator
After using the map function to obtain the iterator, how do we view its contents?
- Directly output the iterator, map object and its physical address:
print(iterator)
# 输出:<map object at 0x7fa7f6165470>
- Use a for loop to iterate over iterator:
for x in iterator:
print(x)
# 输出:
# 200
# 400
# 600
- Convert the iterator to a list using the list() function:
print(list(iterator))
# 输出:[200, 400, 600]
2. Map() function example
Next we continue to look at a few examples of using the map() function to manipulate lists.
Example 1: Use the map() function to operate on a list of strings
The following example uses the map() function to capitalize each element in a string list and then returns a new list:
names = ['david', 'peter', 'jenifer']
new_names = map(lambda name: name.capitalize(), names)
print(list(new_names))
The output is as follows:
['David', 'Peter', 'Jenifer']
Example 2: Use the map() function to operate on a list of tuples
Suppose there is the following shopping cart list consisting of multiple tuples:
carts = [['SmartPhone', 400],
['Tablet', 450],
['Laptop', 700]]
We need to calculate the tax amount for each product, and the tax rate is 10%. At the same time, we need to add the tax amount as a third element to each product information. The final returned list is as follows:
[['SmartPhone', 400, 40.0],
['Tablet', 450, 45.0],
['Laptop', 700, 70.0]]
To do this, we can use the map() function to create a new element with the tax amount as its value:
carts = [['SmartPhone', 400],
['Tablet', 450],
['Laptop', 700]]
TAX = 0.1
carts = map(lambda item: [item[0], item[1], item[1] * TAX], carts)
print(list(carts))
References
- [Python Basics Tutorial] Chapter 37 map() function: https://zhuanlan.zhihu.com/p/474884078