Python简单Min/Max函数写法
时间:2013-04-05 09:29 来源: IT技术网 作者:python

#Python MIN max
#可变长度参数,利用lambda定义函数
lessthan = lambda x, y:x < y;
greaterthan = lambda x, y:x > y;
def minmax(test, *args):
res = args[0]
for arg in args[1:]:
if test(arg, res):
res = arg
return res
print (minmax(lessthan, 4, 2, 1, 3, 4, 5, 6, 7)) #will print 1
print (minmax(greaterthan, 4, 2, 1, 3, 4, 5, 6, 7)) #will print 7
本文来源 我爱IT技术网 http://www.52ij.com/jishu/669.html 转载请保留链接。
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
