The map function in Python

Source: Internet
Author: User

1. Introduction

Python provides built-in function map (), receives two parameters, one is a function, one is a sequence,
Map functions The incoming function to each element of the sequence sequentially, and returns the result as a new list
Back.
For example:

(1) 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, we can use the map () function to complete this calculation:

def f (x):    return x * XPRINT (list (map (f, [1, 2, 3, 4, 5, 6, 7, 8, 9]))

Output Result:
[1, 4, 9, ten, +,----

It can also be written in conjunction with Lambda:

Print map (lambda x:x**2, list)

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

(2) string in list converted to int

List2 = [' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ']print (list (map (lambda x:int (x), List2)))

Output Result:

[1, 2, 3, 4, 5]

With the map () function, you can convert a list to another list, just pass in the conversion function.
Because the list contains elements that can be of any type, map () can not only handle lists that contain only numeric values, but in fact it can handle lists of any type, as long as the incoming function f can handle the data type.

2. Note (the problem with the map function in Python3):

In Python2, the map function returns a list of lists, such as code:

>>> def f (x, Y): return (x, y)  >>> l1 = [0, 1, 2, 3, 4, 5, 6]  

The returned results are as follows:

>>> map (F, L1, L2)  [(0, ' Sun '), (1, ' Mon '), (2, ' Tue '), (3, ' Wed '), (4, ' Thu '), (5, ' Fri '), (6, ' Sat ')]

However, the results returned in Python3 are as follows:

>>> map (f1, L1, L2)  <map object at 0x00000000021da860>  

If you want to get the result of Python2, that is, to return to the list, then you must use list for map, as follows:

>>> list (map (F1, L1, L2))  
3. Example (Python3 implementation):

< Span style= "COLOR: #000000" If the user entered the English name is not standardized, not according to the first letter uppercase, subsequent letter lowercase rules, please use the map () function, Turn a list (including some nonstandard English names) into a list:< Span style= "COLOR: #800000" > < Span style= "COLOR: #800000" >
Method 1:

def format_name (str): STR1 = Str[0:1].upper () + str[1:].lower () return Str1na Me_list = [' BOB ', ' Tom ', ' IVy ']print (list (map (format_name, name_list))) 

< Span style= "COLOR: #0000ff" > < Span style= "COLOR: #800000" > < Span style= "COLOR: #800000" > < Span style= "COLOR: #800000" > Method 2:

def format_name1 (str): STR1 = Str.capitalize () return str1name_list = [' BOB ', ' Tom ', ' IVy ']print (list (map (format_name1, name_list))) 

< Span style= "COLOR: #0000ff" > < Span style= "COLOR: #800000" > < Span style= "COLOR: #800000" > < Span style= "COLOR: #800000" > Method 3:

def format_name1 (str):    str1 = Str.title ()    return str1name_list = [' BOB ', ' Tom ', ' IVy ']print (list (map Format_ Name1, name_list)))

< Span style= "COLOR: #0000ff" > < Span style= "COLOR: #800000" > < Span style= "COLOR: #800000" > < Span style= "COLOR: #800000" > output result:

< Span style= "COLOR: #0000ff" > < Span style= "COLOR: #800000" > < Span style= "COLOR: #800000" > < Span style= "COLOR: #800000" >[' Bob ', ' Tom ', ' Ivy ']

The map function in Python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.