This article allows you to thoroughly grasp [Map function in Python]

You should have heard that the application of Python allows you to reduce the repetitive workload of a day to a few minutes or even less. Since then, free working hours and study more and more efficient working methods. Further improve work efficiency and make work more brilliant. This isn't an ad, this is real.
  
This article will explore the map function in Python with you, so that you can understand the principle of this function in the shortest time. You can also use the fragmented time to consolidate this function to make you more efficient in the process of processing.
  
insert image description here
  
  

1. Definition of map function

  
The map function is a commonly used built-in function in Python, which maps the specified sequence according to the provided function. It can be used to replace the for loop statement to iterate over all specified elements without using any loops, making the code look more concise. Its basic call syntax is as follows:

map(func, *iterables)

func: function.

*iterables: One or more sequences.
  
  

Two, map function example

  

Example 1: Squaring each arity in a sequence

  
If we want to use the circular function to square each number in the sequence, we can use the following code:

new_list = []
for i in [3, 4, 5, 6]:
    new_list.append(i**2)
print(new_list)

got the answer:

[9, 16, 25, 36]

And use the map function to do it directly with one line of code, as follows:

list(map(lambda  x:x**2, [3, 4, 5, 6]))

got the answer:

[9, 16, 25, 36]

Among them, lambda x:x**2 is a function, [3, 4, 5, 6] is the original sequence, and the returned result is the mapping of the original sequence according to the function. However, the result of the map can only be displayed through the list function.

  

Example 2: Find the sum of corresponding elements in two sequences

  
Some people may say that a is a sequence of numbers, and b is another sequence of numbers. It is not easy to find the sum of the two sequences, just a+b. Then let's do a small experiment to see what will be obtained by directly adding + to two numbers in python. The code is as follows:

a = [2, 6, 3]
b = [3, 4, 5]
a + b

got the answer:

[2, 6, 3, 3, 4, 5]

It can be found that the two sequence of numbers in python is +, and the two sequence of numbers are directly spliced ​​and returned. Clever, have you thought about how to use the map function? The specific code is as follows:

a = [2, 6, 3]
b = [3, 4, 5]
list(map(lambda a,b:a+b, a, b))

got the answer:

[5, 10, 8]

Add it manually and you will find that the answer is correct.

  

Example 3: Find the length of each element in the array

  
Friends who know python should know that len(str) means finding the length of str. If you want to ask for the length of each element in the array, how should you write it? The specific code is as follows:

list(map(len, ['white', 'blue', 'green', 'yellow']))

got the answer:

[5, 4, 5, 6]

A manual check will reveal that the answer is correct.

  

Example 4: Convert each English element in the sequence to the corresponding uppercase

  
Friends who know python should know that str.upper() means converting the letters in str to the corresponding uppercase. What should I do if I want to convert the English in each element in the array to the corresponding uppercase? The specific code is as follows:

list(map(lambda x:x.upper(), ['white', 'blue', 'green', 'yellow']))

got the answer:

['WHITE', 'BLUE', 'GREEN', 'YELLOW']

A manual check will reveal that the answer is correct.

  

Example 5: Converting a String to a Numeric Array

  
The specific code for converting a string to a numerical array is as follows:

list(map(int, '789'))

got the answer:

[7, 8, 9]

A manual check will reveal that the answer is correct.

  

Example 6: Extracting keys in a dictionary

  
How many ways can you think of if you want to extract the keys in the dictionary? This article provides two methods for your reference.
  
method one:

Apply the function that comes with the dictionary to extract, the specific code is as follows:

dict_1 = {
    
    '星期一': '吃榴莲', '星期二': '吃葡萄' , '星期三': '吃西瓜', '星期四': '吃樱桃', '星期五': '吃波罗蜜', '星期六': '吃葡萄', '星期天': '吃猕猴桃'}
list(dict_1.keys())

got the answer:

['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期天']

A manual check will reveal that the answer is correct.
  
Method Two:

Apply the map function to extract, the specific code is as follows:

list(map(str, {
    
    '星期一': '吃榴莲', '星期二': '吃葡萄' , '星期三': '吃西瓜', '星期四': '吃樱桃', '星期五': '吃波罗蜜', '星期六': '吃葡萄', '星期天': '吃猕猴桃'}))

got the answer:

['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期天']

It can be found that the results obtained by method 2 and method 1 are consistent. From the above several cases, it can be found that the application of the map function can make the code more concise.
  
So far, the map function in Python has been explained. If you want to know more about functions in Python, you can read the related articles of the "Learning Python" module in the official account.

  
You may be interested in:
Draw Pikachu with Python
Draw a word cloud map
with Python Draw 520 eternal heart beats with Python Python face recognition - you are the only one
in my eyes With sound and text) Use the py2neo library in Python to operate neo4j and build a relationship map Python romantic confession source code collection (love, rose, photo wall, confession under the stars)



Long press (scan) to recognize the QR code above to learn more Python and modeling knowledge, making your study and work more brilliant.

Guess you like

Origin blog.csdn.net/qq_32532663/article/details/125246898