2012-09-27 46 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循环,因为它是做事的正确方法。