当前位置:主页 > 软件编程 > Python代码 >

python实现列表中最大最小值输出的示例

时间:2020-10-02 10:19:55 | 栏目:Python代码 | 点击:

如下所示:

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('测试成功!')

您可能感兴趣的文章:

相关文章