可能重複:
Converting a for loop to a while loopPython的轉換for循環變成一個while循環
我有這樣的一個循環,我做我不知道我怎麼會這麼寫它會與一個while循環一起工作。
def scrollList(myList):
negativeIndices=[]
for i in range(0,len(myList)):
if myList[i]<0:
negativeIndices.append(i)
return negativeIndices
到目前爲止,我有這個
def scrollList2(myList):
negativeIndices=[]
i= 0
length= len(myList)
while i != length:
if myList[i]<0:
negativeIndices.append(i)
i=i+1
return negativeIndices
爲什麼?不,真的,爲什麼? –
@ user1690198我會先看看循環是如何工作的 - 直接在列表上循環,而不是在一系列的痕跡上循環。 (''[index [index for index,value in enumerate(myList)if value <0]'') –
'scrollList = lambda lst:[i for i,v in enumerate(lst)if v <0]' – phihag