python高次関数

4646 ワード

pythonでは関数名も変数名であり、関数を変数に割り当てることができます.
f = print()
f('hello python')

すなわち、関数は別の関数のパラメータとしてもよい
def maxNum(x, y):
	if x > y
	   return x
	else 
	   return y

def getMaxNum(x, y, f):

	return f(x, y)

max = getMaxNum(5, 6, maxNum)

pythonは強力な高次関数をいくつか持っています
Map map
(
function, 
iterable, 
...
)
Return an iterator that applies function to every item of iterable, yielding the results. If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel. With multiple iterables, the iterator stops when the shortest iterable is exhausted. For cases where the function inputs are already arranged into argument tuples, see  itertools.starmap() . その公式ドキュメントから、mapは1つの関数をパラメータとして受信し、この関数は1つのパラメータのみを受信し、もう1つのパラメータはiterableタイプであり、mapの役割は、入力された関数をiterableの各要素に作用させ、新しいiteratorを返すことである.
>>> def doubleNum(x):
...    return x*2
... 
>>> l=list(range(1,11))
>>> l
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> double_l=map(doubleNum, l)
>>> double_l
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

Reduce functools. reduce
(
function, 
iterable
[, 
initializer
]
)
Apply function of two arguments cumulatively to the items of sequence, from left to right, so as to reduce the sequence to a single value. For example,  reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])  calculates  ((((1+2)+3)+4)+5) . The left argument, x, is the accumulated value and the right argument, y, is the update value from the sequence. If the optional initializer is present, it is placed before the items of the sequence in the calculation, and serves as a default when the sequence is empty. If initializer is not given and sequence contains only one item, the first item is returned. reduceは2つのパラメータを入力し、1つは2つのパラメータの関数と1つのiterableクラスを受信して1つの結果を返し、ドキュメントにも1つの例を与え、1つのx+yの関数を1から5のlistに作用させ、1から5の和を求めることに相当する.
 
>>> l=list(range(1,101))
>>> l
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
>>> reduce(lambda x,y:x+y, l)
5050

Max max
(
arg1, 
arg2, 
*args
[, 
key
]
)
Return the largest item in an iterable or the largest of two or more arguments.
If one positional argument is provided, it should be an iterable. The largest item in the iterable is returned. If two or more positional arguments are provided, the largest of the positional arguments is returned.
There are two optional keyword-only arguments. The key argument specifies a one-argument ordering function like that used for  list.sort() . The default argument specifies an object to return if the provided iterable is empty. If the iterable is empty and default is not provided, a ValueError  is raised.
If multiple items are maximal, the function returns the first one encountered. This is consistent with other sort-stability preserving tools such as  sorted(iterable, key=keyfunc, reverse=True)[0]  and  heapq.nlargest(1, iterable, key=keyfunc) .
utterableの最大要素または伝達パラメータの最大値を返します.複数のパラメータ、1つのlistまたはlist、その他のパラメータを直接伝達できます.
>>> max('a','b')
'b'
>>> l=[3,5,9]
>>> max(l)
9
>>> max(l,'a')
'a'
>>> 

関数名は変数として関数パラメータとして使用でき、必然的に関数の戻り値として使用できます.
>>> def getMax(l):
...    return max(l)
... 
>>> f=getMax(list(range(1,6)))
>>> f
5