【发布时间】:2020-11-28 09:28:40
【问题描述】:
我正在创建一个函数,我必须递归地在列表列表中找到最小值。例如,包含 [1, 2, 3, [4, 5]] 的列表将返回 1。
我已经写了一些代码。但是,我不知道如何遍历列表并将最小的数字与列表进行比较。
我在下面的代码中添加了一条注释,其中我的函数出现错误: def recMin(nestedLis):
lowest = 1000000
if len(nestedLis) < 2:
return nestedLis
else:
# This function works until it hits a list in a list (ex. [1, 2])
# because it cannot compare a list to an int. I don't know how to fix this
if nestedLis[0] < lowest:
lowest = nestedLis[0]
print(lowest)
return lowest + recMin(nestedLis[1:])
任何帮助将不胜感激!
【问题讨论】:
标签: python list loops recursion