• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Map

python 搞代码 4年前 (2022-01-09) 22次浏览 已收录 0个评论

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

 

 


例: 自定义函数模拟内置函数map,列表自增减1及本文来源gaodai$ma#com搞$$代**码网平方


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

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>

自定义函数实现

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))

 

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

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))

 

 


以上就是Map的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Map
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址