-1

可能重复:
将 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)

嗨,我有这段代码,在 for 循环下效果很好。它根据用户输入的内容返回正面或负面的元素。我需要让它在一个while循环下工作,我不知道如何让它工作而不被困在一个while循环中。

任何想法或提示将不胜感激,谢谢!

4

6 回答 6

1

冒着因未严格遵守您的问题而遭到反对的风险,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]
>>> 

理解中的条件语法看起来很复杂,因为您的optionto effect 映射很差。在 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]
>>>     

请注意这是多么灵活:使代码适应完全不同的过滤条件变得微不足道。

于 2012-09-27T02:03:58.067 回答
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)
于 2012-09-27T01:32:46.123 回答
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 循环,因为它是做事的正确方式。

于 2012-09-27T04:05:48.550 回答
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)
于 2012-09-27T01:34:41.857 回答
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
于 2012-09-27T01:36:37.803 回答
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)
于 2012-09-27T01:42:52.370 回答