Map

PHP中文网
Lepaskan: 2017-06-19 10:39:51
asal
1127 orang telah melayarinya

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

 

 

Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

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

Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

内置函数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>
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

自定义函数实现

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))
Salin selepas log masuk

 

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

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))
Salin selepas log masuk

 

 

Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

Atas ialah kandungan terperinci Map. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan