Python iterative practice

Please use the minimum and maximum iterations to find a list, and returns a tuple:

from collections import Iterable
def findMinAndMax(L):
    if L: # L不为空
        if isinstance(L, Iterable): # L是可迭代对象
            return (min(L), max(L))

    return (None, None)

# 测试
if findMinAndMax([]) != (None, None):
    print('测试失败_1!')
elif findMinAndMax([7]) != (7, 7):
    print('测试失败_2!')
elif findMinAndMax([7, 1]) != (1, 7):
    print('测试失败_3!')
elif findMinAndMax([7, 1, 3, 9, 5]) != (1, 9):
    print('测试失败_4!')
else:
    print('测试成功!')

If you have a better idea, welcome the exchange of learning

Published 110 original articles · won praise 2 · Views 3742

Guess you like

Origin blog.csdn.net/qq_40041064/article/details/105096786