Map

PHP中文网
Release: 2017-06-19 10:39:51
Original
1085 people have browsed it

python3.6

map(func, *iterables) --> map object<br><br>Make an iterator that computes the function using arguments from<br>each of the iterables.  Stops when the shortest iterable is exhausted.<br><br><br>
Copy after login
map(func, *iterables) --> map object
Copy after login
  • func is a lambda anonymous function with simple logic. Complex logic needs to be designed by yourself;
  • *iterables Iterable objects
  • The result obtained by the map function is also an iterable object, but it can only be iterated once.

Copy after login
Copy after login
Copy after login
Copy after login

Example: Custom function simulates the built-in function map, the list increases and decreases by 1 and squares

Copy after login
Copy after login
Copy after login
Copy after login

The built-in function map implements the list's automatic increase and decrease by 1 and square

li = [1, 2, 3, 4, 5, 6, 7, 8, 9<span style="color: #000000">]
</span><span style="color: #008000">#</span><span style="color: #008000"> 自增1</span>
<span style="color: #0000ff">print</span>(list(map(<span style="color: #0000ff">lambda</span> x: x + 1<span style="color: #000000">, li)))
</span><span style="color: #008000">#</span><span style="color: #008000"> 自减1</span>
<span style="color: #0000ff">print</span>(list(map(<span style="color: #0000ff">lambda</span> x: x - 1<span style="color: #000000">, li)))
</span><span style="color: #008000">#</span><span style="color: #008000"> 平方</span>
<span style="color: #0000ff">print</span><span style="color: #000000">(
    list(
        map(</span><span style="color: #0000ff">lambda</span> x: x ** 2<span style="color: #000000">, li)
    )
)</span>
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

Custom function implementation

li = [1, 2, 3, 4, 5, 6, 7, 8, 9<span style="color: #000000">]


</span><span style="color: #008000">#</span><span style="color: #008000"> 自增1</span>
<span style="color: #0000ff">def</span><span style="color: #000000"> add1(x):
    </span><span style="color: #0000ff">return</span> x + 1


<span style="color: #008000">#</span><span style="color: #008000"> 自减1</span>
<span style="color: #0000ff">def</span><span style="color: #000000"> red1(x):
    </span><span style="color: #0000ff">return</span> x - 1


<span style="color: #008000">#</span><span style="color: #008000"> 平方</span>
<span style="color: #0000ff">def</span><span style="color: #000000"> square(x):
    </span><span style="color: #0000ff">return</span> x ** 2


<span style="color: #0000ff">def</span><span style="color: #000000"> map_test(func, l):
    tl </span>=<span style="color: #000000"> []
    </span><span style="color: #0000ff">for</span> i <span style="color: #0000ff">in</span><span style="color: #000000"> l:
        tl.append(func(i))
    </span><span style="color: #0000ff">return</span><span style="color: #000000"> tl

</span><span style="color: #008000">#</span><span style="color: #008000"> 调用上面定义的函数</span>
<span style="color: #0000ff">print</span><span style="color: #000000">(map_test(add1, li))
</span><span style="color: #0000ff">print</span><span style="color: #000000">(map_test(red1, li))
</span><span style="color: #0000ff">print</span>(map_test(square, li))
Copy after login

Custom function + anonymous function implementation

li = [1, 2, 3, 4, 5, 6, 7, 8, 9<span style="color: #000000">]

</span><span style="color: #0000ff">def</span><span style="color: #000000"> map_test(func, l):
    tl </span>=<span style="color: #000000"> []
    </span><span style="color: #0000ff">for</span> i <span style="color: #0000ff">in</span><span style="color: #000000"> l:
        tl.append(func(i))
    </span><span style="color: #0000ff">return</span><span style="color: #000000"> tl

</span><span style="color: #0000ff">print</span>(map_test(<span style="color: #0000ff">lambda</span> x: x + 1<span style="color: #000000">, li))
</span><span style="color: #0000ff">print</span>(map_test(<span style="color: #0000ff">lambda</span> x: x - 1<span style="color: #000000">, li))
</span><span style="color: #0000ff">print</span>(map_test(<span style="color: #0000ff">lambda</span> x: x ** 2, li))
Copy after login

Copy after login
Copy after login
Copy after login
Copy after login

The above is the detailed content of Map. For more information, please follow other related articles on the PHP Chinese website!

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