Python map()関数
例
タプル内の各単語の長さを計算します。
def myfunc(n):
return len(n)
x = map(myfunc, ('apple',
'banana', 'cherry'))
定義と使用法
このmap()
関数は、イテラブル内のアイテムごとに指定された関数を実行します。アイテムはパラメータとして関数に送信されます。
構文
map(function, iterables)
パラメータ値
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. |
その他の例
例
2つの反復可能なオブジェクトを関数に送信して、新しい果物を作ります。
def myfunc(a, b):
return a + b
x = map(myfunc, ('apple', 'banana',
'cherry'), ('orange', 'lemon', 'pineapple'))