Home  >  Article  >  Backend Development  >  What does map mean in python?

What does map mean in python?

(*-*)浩
(*-*)浩Original
2019-07-19 16:17:3936830browse

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, ...)

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])

Output result:

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

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!

Statement:
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