如下所示:
def findMinAndMax(L): maxL = None minL = None if L: maxL = L[0] minL = L[0] for i in range(len(L)): if L[i] > maxL: maxL = L[i] elif L[i] < minL: minL = L[i] else: maxL = None minL = None return (minL, maxL)
测试:
# 测试 if findMinAndMax([]) != (None, None): print('测试失败!') elif findMinAndMax([7]) != (7, 7): print('测试失败!') elif findMinAndMax([7, 1]) != (1, 7): print('测试失败!') elif findMinAndMax([7, 1, 3, 9, 5]) != (1, 9): print('测试失败!') else: print('测试成功!')
以上这篇python实现列表中最大最小值输出的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。