关闭

python 高阶函数

标签: python高阶函数python高阶函数
92人阅读 评论(0) 收藏 举报
分类:

在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(functioniterable...)

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接收一个函数作为参数并且这个函数只接收一个参数,另一个参数为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(functioniterable[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传入两个参数,一个接收两个参数的函数和一个iterable类并返回一个结果,文档中也给了一个例子,把一个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(arg1arg2*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, aValueError 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中最大的元素或者所传参数中的最大值,可以传多个参数  、直接传一个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



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3483次
    • 积分:289
    • 等级:
    • 排名:千里之外
    • 原创:26篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类