使用for循环查找最小数及其位置的示例:
def smallest(list):
smallest = 1000000
smallestposition=-1
for pos,value in enumerate(list):
if(value < smallest):
smallest = value
smallestposition = pos
return smallest,smallestposition
print smallest([23,444,222,111,56,7,45])
Tags:
在递归函数中使用
enumerate()
是没有意义的,因为枚举是迭代的,这是递归的“相反”。在此函数的递归版本可以是:
相关问题 更多 >
编程相关推荐