python map function (23)

    Up to now, in fact, we have contacted a lot of python built-in functions, and the map function is one of them, according to the specified map function is a function to do the mapping for the specified sequence, but also improve process efficiency using the map function in the development of one of the ways.

smell it

A. Syntax definition

'''
function: function name
iterable: a plurality of sequence or sequences, in fact, this is the function corresponding to arguments
'''
map(function, iterable, ...)

 

 

    parameter:
    function : function name
    Iterable : a plurality of sequence or sequences, in fact, this is the function corresponding to arguments
    Return value :
        The return value is an iterator, pay attention to the results returned only one iteration, if you need to use multiple save and process the results in advance.
 
 

II. Practical exercises

    1. Using the map function

# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author: Why grief
@Blog (personal blog address): shuopython.com
@WeChat Official Account (micro-channel public number): ape says python
@Github:www.github.com
 
@File:python_map.py
@Time:2019/10/7 19:48
 
@Motto: short step a thousand miles, no small streams converge into a wonderful required the ocean, the program of life relentlessly accumulate!
"""
 
DEF func1 (X):
     # sequence by multiplying each element 10 and returns a 
    return X 10 *
 
 
'''
    map () will do the mapping function in accordance with the specified sequence provided.
    Each element in the sequence of calls func1 function that returns a new list.
'' ' 
X = Map (func1, Range (0,10 ))
 Print (List (X))
 # Map iterator function returns only one iteration will be cleared automatically after iteration 
Print (List (X))
 
Print ( " *** " * 20 )
 # to save the map iterator function returns the converted list, you can use many times 
the y-List = (map (func1, the Range (0,10 )))
 Print (the y-)
 Print (the y- )

 

Output:

[0, 10, 20, 30, 40, 50, 60, 70, 80, 90]
[]
************************************************************
[0, 10, 20, 30, 40, 50, 60, 70, 80, 90]
[0, 10, 20, 30, 40, 50, 60, 70, 80, 90]

 

 

2.map function with an anonymous function together

# Map function with the use of anonymous function 
X = List (Map ( the lambda A: A * 10, Range (0,10))) # sequence multiplies each element 10 
Print (X)
 
# Map function with the use of anonymous functions, anonymous function has two parameters, the parameter map should also pass two sequences 
Y = List (map ( the lambda A, B: A + B, [1,2,3,5,6, 7], [10,20,30,50,60,70])) # two sequences are added 
Print (Y)

 

    Output:

[0, 10, 20, 30, 40, 50, 60, 70, 80, 90]
[11, 22, 33, 55, 66, 77]

 

    Note: If the function map () function is a plurality of parameters, the parameter passing time map should be transmitted more sequences.

 

Code

 

III. Efficiency Comparison

    The following comparison of efficiency, into ten million data to the list, under relatively time-consuming cases:

import time
list1 = list()
 
# 普通for循环
start = time.clock()
for i in range(0,10000000):
    list1.append(i)
print("普通for循环耗时:",time.clock() - start)
 
# 列表推导式
list1.clear()
start = time.clock()
list1 = [i for i in range(0,10000000)]
print("列表推导式循环耗时:",time.clock() - start)
 
# map映射函数
list1.clear()
start = time.clock()
List1 = List (Map ( the lambda X: X, Range (0,10000000 )))
 Print ( " Map consuming mapping function: " , time.clock () - Start)

 

     Output:

Common for loop Processed: 1.1869014999999998 
listing derived circulation time: 0.5339119999999999 
Map consuming mapping function: 0.9047431000000001

 

    The test results opinion: deriving a listing of formula efficiency> map mapping function> Common for loop

copy and paste

 

IV. Key summary

    1.map function parameters is made and a sequence or function of a plurality of sequences;

    The results 2.map processing function is an iterator, and only one iteration, if used more than once, save in advance;

 

 

you may also like:

    1.python built-in function input / eval

    2.python return logic expression is determined

    3. anonymous function

 

    Reproduced please specify: ape say Python  »  Python function of the Map

 

Technical exchanges, business cooperation please contact bloggers
Scan code or search: ape say python
No public python tutorial
Ape say python
Micro-channel public number  sweep the attention

Guess you like

Origin www.cnblogs.com/shuopython/p/11900865.html
Recommended