【Python】map, filter, reduceの使い方

スポンサーリンク
スポンサーリンク

map

myArray = [0, 1, 2, 3, 4, 5]
# それぞれ2倍する
result = map(lambda value: value * 2, myArray)
print(list(result))
# [ 0, 2, 4, 6, 8, 10 ]

filter

myArray = [0, 1, 2, 3, 4, 5]
# 3未満の数字を抽出する
result = filter(lambda value: value < 3, myArray)
print(list(result))
# [ 0, 1, 2 ]

reduce

import functools
myArray = [0, 1, 2, 3, 4, 5]
# 合計値を求める
result = functools.reduce(lambda a, b: a+b, myArray)
print(result)

参考

組み込み関数
Python インタプリタには数多くの関数と型が組み込まれており、いつでも利用できます。それらをここにアルファベット順に挙げます。,,,, 組み込み関数,,, A, abs(), aiter(), all(), anext(), any(), ascii(),, B, bin(), bool(), breakpoint...
functools — Higher-order functions and operations on callable objects
Source code: Lib/functools.py The functools module is for higher-order functions: functions that act on or return other functions. In general, any callable obje...
組み込み関数
Python インタプリタには数多くの関数と型が組み込まれており、いつでも利用できます。それらをここにアルファベット順に挙げます。,,,, 組み込み関数,,, A, abs(), aiter(), all(), anext(), any(), ascii(),, B, bin(), bool(), breakpoint...

コメント

タイトルとURLをコピーしました