Home > Backend Development > Python Tutorial > What does map mean in python?

What does map mean in python?

(*-*)浩
Release: 2019-07-19 16:17:39
Original
37068 people have browsed it

The prototype of the map function is map(function, iterable, ...), and its return result is a list.

What does map mean in python?

map() is a built-in higher-order function in Python. It receives a function f and a lis, and applies the function f to the list in sequence. On each element of , get a new list and return it.

map() will map the specified sequence according to the provided function. It is a built-in function (recommended learning: Python video tutorial)

The first parameter function calls the function function with each element in the parameter sequence and returns a new list containing the return value of each function function.

Syntax

map(function, iterable, ...)
Copy after login

For example, for list [1, 2, 3, 4, 5, 6, 7, 8, 9]

If you want to square each element of the list, you can use the map() function:

Therefore, we only need to pass in the function f(x)= x*x, you can use the map() function to complete this calculation:

def f(x):
 return x*x
print map(f, [1, 2, 3, 4, 5, 6, 7, 8, 9])
Copy after login

Output result:

[1, 4, 9, 10, 25, 36, 49, 64, 81]
Copy after login

Note: The map() function does not change the original list, but Return a new list.

Using the map() function, you can convert one list into another list by passing in the conversion function.

Since the elements contained in the list can be of any type, map() can not only handle lists containing only numerical values, in fact it can handle lists containing any type, as long as the function passed in f can handle this data type.

For more Python related technical articles, please visit the Python Tutorial column to learn!

The above is the detailed content of What does map mean in python?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template