我在for循环中迭代,构造一个列表,同时将该列表的最后一个数字与另一个数字进行比较。你知道吗
我想让我的代码查看列表的最后一项是否比它要比较的项小,如果是,则将其添加到列表的末尾,然后继续。你知道吗
如果列表的最后一项较大,我想弹出列表的最后一项。那么,我想对其附加同样的条件。你知道吗
这是我的代码,它不工作,它不会重新检查后弹出列表的最后一项条件。你知道吗
if tempList:
lastNum=tempList[-1]
#############################################
if element < lastNum:
incList.append(tempList)
tempList.pop()
lastNum=tempList[-1]
#############################################
elif lastNum < element:
tempList.append(element)
continue
您可以将其捆绑到一个函数中:
输出:
为什么还要返回列表:链接示例:
输出:
试试这个:
输出: [3,1]
[3,1,4,5]
相关问题 更多 >
编程相关推荐