Discuss / Python / 要注意准备两个return

要注意准备两个return

Topic source

def findMinAndMax(L): if L==[]: return (None,None) else:
min = L[0] max = L[0] for i in L: if i < min: min = i if i > max: max = i return (min,max)
一个返回 return (None,None)给L为[]的情况 另一个return (min,max)为L不为空的情况 有可能len(L)=1,所以要提前给min和max赋值=L[0]

鲲鹏浮游

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

解释的好详细,棒棒哒

大炳子1995

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

还是不太懂这里L[0]的含义,能麻烦你帮我解释下吗?

苏生不语_

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

@大炳子1995 把list的第一个值赋值给min和max,然后和其他的值比较


  • 1

Reply