当前位置:Gxlcms > Python > Map

Map

时间:2021-07-01 10:21:17 帮助过:23人阅读

python3.6

  1. 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>
  1. map(func, *iterables) --> map object
  • func 逻辑简单lambda匿名函数,逻辑复杂需自拟;
  • *iterables 可迭代对象
  • map函数所得的结果也是一个可迭代对象,但是只能遍例一次.

 

 

例: 自定义函数模拟内置函数map,列表自增减1及平方

内置函数map实现列表自增减1及平方

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

自定义函数实现

  1. li = [1, 2, 3, 4, 5, 6, 7, 8, 9<span style="color: #000000">]
  2. </span><span style="color: #008000">#</span><span style="color: #008000"> 自增1</span>
  3. <span style="color: #0000ff">def</span><span style="color: #000000"> add1(x):
  4. </span><span style="color: #0000ff">return</span> x + 1
  5. <span style="color: #008000">#</span><span style="color: #008000"> 自减1</span>
  6. <span style="color: #0000ff">def</span><span style="color: #000000"> red1(x):
  7. </span><span style="color: #0000ff">return</span> x - 1
  8. <span style="color: #008000">#</span><span style="color: #008000"> 平方</span>
  9. <span style="color: #0000ff">def</span><span style="color: #000000"> square(x):
  10. </span><span style="color: #0000ff">return</span> x ** 2
  11. <span style="color: #0000ff">def</span><span style="color: #000000"> map_test(func, l):
  12. tl </span>=<span style="color: #000000"> []
  13. </span><span style="color: #0000ff">for</span> i <span style="color: #0000ff">in</span><span style="color: #000000"> l:
  14. tl.append(func(i))
  15. </span><span style="color: #0000ff">return</span><span style="color: #000000"> tl
  16. </span><span style="color: #008000">#</span><span style="color: #008000"> 调用上面定义的函数</span>
  17. <span style="color: #0000ff">print</span><span style="color: #000000">(map_test(add1, li))
  18. </span><span style="color: #0000ff">print</span><span style="color: #000000">(map_test(red1, li))
  19. </span><span style="color: #0000ff">print</span>(map_test(square, li))

 

自定义函数+匿名函数实现

  1. li = [1, 2, 3, 4, 5, 6, 7, 8, 9<span style="color: #000000">]
  2. </span><span style="color: #0000ff">def</span><span style="color: #000000"> map_test(func, l):
  3. tl </span>=<span style="color: #000000"> []
  4. </span><span style="color: #0000ff">for</span> i <span style="color: #0000ff">in</span><span style="color: #000000"> l:
  5. tl.append(func(i))
  6. </span><span style="color: #0000ff">return</span><span style="color: #000000"> tl
  7. </span><span style="color: #0000ff">print</span>(map_test(<span style="color: #0000ff">lambda</span> x: x + 1<span style="color: #000000">, li))
  8. </span><span style="color: #0000ff">print</span>(map_test(<span style="color: #0000ff">lambda</span> x: x - 1<span style="color: #000000">, li))
  9. </span><span style="color: #0000ff">print</span>(map_test(<span style="color: #0000ff">lambda</span> x: x ** 2, li))

 

 

以上就是Map的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行