Python函数定义。

定义名为“secondLarge”的函数,该函数以一组整数作为参数,然后该函数将识别并返回列表中第二大的数字。如果参数只包含两个数字,函数将返回最小的数字。而如果列表只包含一个数字,函数将返回唯一的数字。在某些特殊情况下,如果形参在列表中包含其他数据类型e.string或float,函数将返回-999。
例如:
[12345.6]返回5[6,8,3,4,6]返回6[53,23]返回23。[13]返回13
[12,'not number,23]返回-99220

你题目的解答代码如下:(如有帮助,望采纳!谢谢! 点击我这个回答右上方的【采纳】按钮)

def secondLarge(li):
    if not all(map(lambda x: isinstance(x,int),li)):
        return -999
    if len(li)==1:
        return li[0]
    li.sort(reverse=True)
    return li[1]

print(secondLarge([6,8,3,4,6]))
print(secondLarge([12,"not number",23]))

主要是对输入的列表进行筛选判断,代码可这样写:

def secondLarge(lst):
    if any(isinstance(x, float) for x in lst) or any(isinstance(x, str) for x in lst) or len(lst)==0:
        res=-999
    elif len(lst)==1:
        res=lst[0]
    else:
        res=sorted(lst)[-2]        
    return res
print(secondLarge([12, 23]))
print(secondLarge([12, 23,'abc']))

如对你有帮助,请点采纳。