Karte

PHP中文网
PHP中文网Original
2017-06-19 10:39:511136Durchsuche

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 ist eine anonyme Lambda-Funktion mit einfacher Logik, die angepasst werden muss
  • *iterables iterierbare Objekte
  • Das von der Kartenfunktion erhaltene Ergebnis ist ebenfalls ein iterierbares Objekt, kann jedoch nur einmal iteriert werden.

Beispiel: Eine benutzerdefinierte Funktion simuliert die integrierte Funktionszuordnung, die Liste erhöht und verringert sich um 1 und quadriert

Die integrierte Funktionszuordnung implementiert die automatische Inkrementierung, Dekrementierung und Quadrierung von Listen um 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>
Benutzerdefinierte Funktionsimplementierung

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

Benutzerdefinierte Funktion + anonyme Funktionsimplementierung

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

Das obige ist der detaillierte Inhalt vonKarte. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn