2012-09-27 44 views
-1

可能重複:
Converting a for loop to a while loop如何利用這個for循環,使之成爲一個while循環蟒蛇

def splitList(myList, option): 
    snappyList = [] 
    for i in myList: 
     if option == 0: 
      if i > 0: 
       snappyList.append(i) 
     if option == 1: 
      if i < 0: 
       snappyList.append(i) 
    return (snappyList) 

您好,我有這樣的代碼下的偉大工程循環。它根據用戶輸入的內容返回正面或負面的元素。我需要在while循環下得到這個工作,我不確定如何讓它在沒有被while循環捕獲的情況下工作。

任何想法或提示,將不勝感激,謝謝!

+2

如果你在一個表格中解釋_why_你需要它,以及你希望爲你做什麼,你可能會得到更有用的答案。明顯的轉換成while循環只是減少了循環的可讀性。 –

+1

這是一個奇怪的問題,但它並不意味着你不應該問這個問題。 :)我在想更合理的問題,因爲這是Python,會是:我怎麼讓這個代碼更Pythonic?然後有人會進來一個列表理解.... –

+0

作爲一個方面說明:你可能想要分割成兩個循環基於選項,以避免如果循環內。 –

回答

1

嘗試以下操作:

def splitList(myList, option): 
    snappyList = [] 
    i = 0 
    while i < len(myList): 
     if option == 0: 
      if myList[i] > 0: 
       snappyList.append(myList[i]) 
     if option == 1: 
      if myList[i] < 0: 
       snappyList.append(myList[i]) 
     i+=1 
    return (snappyList) 
+0

非常感謝!它工作出色! – Subtlyinyourmind

+0

您的歡迎夥伴;) – Littm

0
def splitList(myList, option): 
    snappyList = [] 
    myListCpy=list(myList[:]) #copy the list, in case the caller cares about it being changed, and convert it to a list (in case it was a tuple or similar) 
    while myListCpy: #There's at least one element in the list. 
     i=myListCpy.pop(0) #Remove the first element, the rest continues as before. 
     if option == 0: 
      if i > 0: 
       snappyList.append(i) 
     if option == 1: 
      if i < 0: 
       snappyList.append(i) 
    return (snappyList) 
0
def splitList(myList, option): 
    snappyList = [] 
    while len(myList) > 0: 
     i = myList.pop() 
     if option == 0: 
      if i > 0: 
       snappyList.append(i) 
     if option == 1: 
      if i < 0: 
       snappyList.append(i) 
    return snappyList 
+0

此解決方案的問題是,如果調用方期望myList保持不變,您將打破該程序的其餘部分。首先需要創建列表的副本,因爲如果python中的所有內容都是True,如果它不是False,並且空列表是False,那麼檢查len(List)> 0是否比它需要的慢,您可以只需檢查List是否爲列表,如果列表不爲空則爲True。 – Perkins

0
def splitList(myList, option): 
    snappyList = [] 
    myList_iter = iter(myList) 
    sentinel = object() 
    while True: 
     i = next(myList_iter, sentinel) 
     if i == sentinel: 
      break 
     if option == 0: 
      if i > 0: 
       snappyList.append(i) 
     if option == 1: 
      if i < 0: 
       snappyList.append(i) 
    return (snappyList) 

或者,您也可以使用異常處理程序,而不是定點

def splitList(myList, option): 
    snappyList = [] 
    myList_iter = iter(myList) 
    while True: 
     try: 
      i = next(myList_iter) 
     except StopIteration: 
      break 
     if option == 0: 
      if i > 0: 
       snappyList.append(i) 
     if option == 1: 
      if i < 0: 
       snappyList.append(i) 
    return (snappyList) 
1

吸引downvotes的風險不嚴格遵守你的問題, Python比其他更傳統的語言具有更好的(簡單)循環設施。 (我也意識到,根據今天早上有非常類似的問題,這可能是家庭作業)。學習while循環如何工作顯然有一定價值,但在Python中這樣做會掩蓋其他工具。例如,您例如,使用單個列表理解:

def splitList2(myList, option): 
    return [v for v in myList if (1-2*option)*v > 0] 

print(splitList2([1,2,3,-10,4,5,6], 0)) 
print(splitList2([1,2,3,-10,4,5,6], 1)) 

輸出:

[1, 2, 3, 4, 5, 6] 
[-10] 
>>> 

條件的在修真的語法只是看起來很複雜,因爲你要的效果option映射較差。在Python,正如在許多其他動態和函數式語言,你可以通過直接比較函數:

def splitList3(myList, condition): 
    return [v for v in myList if condition(v)] 

print(splitList3([1,2,3,-10,4,5,6], lambda v: v>0)) 
print(splitList3([1,2,3,-10,4,5,6], lambda v: v<0)) 
print(splitList3([1,2,3,-10,4,5,6], lambda v: v%2==0)) 

[1, 2, 3, 4, 5, 6] 
[-10] 
[2, -10, 4, 6] 
>>>  

注意如何更靈活的是:它變得微不足道的代碼適應一個完全不同的過濾條件。

0

這裏的實際分裂列表的簡潔的方式,而不是僅僅將其過濾:

from operator import ge,lt 
def splitlist(input, test=ge, pivot=0): 
    left = [] 
    right = [] 
    for target, val in (((left if test(n, pivot) else right), n) for n in input): 
     target.append(val) 

    return left, right 

你會注意到它使用一個for循環,因爲它是做事的正確方法。