如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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) |
测试:
1
2
3
4
5
6
7
8
9
10
11
|
# 测试 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实现列表中最大最小值输出的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/Andrew_jdw/article/details/82665025