Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the map function in python

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces you how to understand the map function in python, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Today, we introduce a very easy-to-use built-in function in Python that is easy to write but can implement seemingly complex functions.

It is the map function.

The prototype of the map function is map (function, iterable, …) Whose return result is a map object

The parameter function passes a function name, which can be built-in or customized by python. The parameter iterable passes an object that can be iterated, such as lists, tuples, strings, etc.

This function means that function is applied to every element of iterable, and the result is returned in the form of map.

Notice that iterable is followed by an ellipsis, meaning that many iterable can be passed. If there are additional iterable parameters, take elements from these parameters in parallel and call function.

The following example illustrates the use of map.

Built-in function

Str () is a built-in function for python, and this example changes each element of a list / tuple / string to a str type:

A=list (map (str,'python')) print

['pairing, writing, reading, writing, etc.]

Custom function

Lambda anonymous functions are usually used, which is easy to write.

# use the lambda anonymous function list (map (lambda x: X * * 2, [1,2,3,4,5]))

[1, 4, 9, 16, 25]

Multiple iterable instances

Examples are as follows:

Map (lambda x, y: X + y, [1,3,5,7,9], [2,4,6,8,10])

Pay attention to two details:

The number of parameters in lambda is equal to the number of iterable instances

The number of elements in each iterable does not have to be equal, and the number of elements returned is equal to the length of the shortest iterable.

On how to understand the map function in python to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report