Python map() Functie
Voorbeeld
Bereken de lengte van elk woord in de tupel:
def myfunc(n):
return len(n)
x = map(myfunc, ('apple',
'banana', 'cherry'))
Definitie en gebruik
De map()
functie voert een gespecificeerde functie uit voor elk item in een iterable. Het item wordt als parameter naar de functie gestuurd.
Syntaxis
map(function, iterables)
Parameterwaarden
Parameter | Description |
---|---|
function | Required. The function to execute for each item |
iterable | Required. A sequence, collection or an iterator object. You can send as many iterables as you like, just make sure the function has one parameter for each iterable. |
Meer voorbeelden
Voorbeeld
Maak nieuw fruit door twee itereerbare objecten naar de functie te sturen:
def myfunc(a, b):
return a + b
x = map(myfunc, ('apple', 'banana',
'cherry'), ('orange', 'lemon', 'pineapple'))