Discuss / Python / 为了简洁

为了简洁

Topic source

    L.sort();

    try:

        return (L[0], L[-1]);

    except:

        return (None, None);

用到了sort函数排序,主要是为了简洁,当然只能适用于这个题目的情况,如果想做成方法需要再优化一下,主要是不想用两层循环来处理,看着就难受,不简洁,希望大家能提供更简洁明了的代码

你这没用到迭代呀

而且 不用两层循环

def findMinAndMax(L):    if len(L) == 0:        Min = Max = None;    else:        Min = Max = L[0];        for num in L:            if num < Min:                Min = num;            if num > Max:                Max = num;    return (Min, Max);

asade69552

#3 Created at ... [Delete] [Delete and Lock User]

用sort()的话直接用内置函数max()、min()就好了

金钟铉

#4 Created at ... [Delete] [Delete and Lock User]

min,max,三行可以了

def findMinAndMax(L):    return (None, None) if (len(L) == 0) else (reduce(lambda x, y: y if (x > y) else x, L), reduce(lambda x, y: x if (x > y) else y, L))from functools import reduce

王胜辉仔

#6 Created at ... [Delete] [Delete and Lock User]

gotta play by the rules, man!


  • 1

Reply