【发布时间】:2016-01-31 11:29:02
【问题描述】:
所以我已经有一段时间了。我正在尝试创建一个函数来检查列表中的数字是否在增加。例如 [1, 2, 3] 为 True,但 [1, 3, 2] 为 False。我已经到了它会说 [1, 2, 3] 是 True,[3, 2, 1] 是 False,但 [1, 3, 2] 仍然是 True 的地步。我认为这只是因为只读取前两个位置?
下面是函数供参考:
def increasing(lst):
index = 0
index2 = index + 1
while index < len(lst):
if lst[index] >= lst[index2]:
index += 1
index2 += 1
return False
else:
while index < len(lst):
if lst[index] < lst[index2]:
index += 1
index2 += 1
return True
【问题讨论】:
-
index有没有可能不变? -
注意单元素和空列表。
标签: python