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

What does map mean in python?

藏色散人
Release: 2019-06-22 10:32:05
Original
11896 people have browsed it

What does map mean in python?

What does map in python mean?

map() in python will do the specified sequence according to the provided function. mapping.

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

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

Parameters

function -- function

iterable -- one or more sequences

Return value

Python 2.x returns a list.

Python 3.x Returns the iterator.

The following example shows how to use map():

>>>def square(x) :            # 计算平方数
...     return x ** 2
... 
>>> map(square, [1,2,3,4,5])   # 计算列表各个元素的平方
[1, 4, 9, 16, 25]
>>> map(lambda x: x ** 2, [1, 2, 3, 4, 5])  # 使用 lambda 匿名函数
[1, 4, 9, 16, 25]
 
# 提供了两个列表,对相同位置的列表数据进行相加
>>> map(lambda x, y: x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10])
[3, 7, 11, 15, 19]
Copy after login

Related recommendations: "Python Tutorial"

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