迭代时从Python中的列表中删除项目

时间:2011-09-27 17:17:53

标签: python list

我正在为锦标赛应用编写循环算法。

当玩家数量为奇数时,我将'DELETE'添加到玩家列表中,但稍后,当我想要从包含'DELETE'的日程表列表中删除所有项目时,我不能 - - 总是留下一个。请看一下代码 - 问题很简单,我想这是关于列表的;我只是看不到它。

"""
Round-robin tournament:
1, 2, 3, 4, | 5, 6, 7, 8    =>  1, 2, 3, 4  => rotate all but 1 =>  1, 5, 2, 3  => repeat =>    1, 6, 5, 2  ...
                    5, 6, 7, 8              6, 7, 8, 4          7, 8, 4, 3
in every round pick l1[0] and l2[0] as first couple, after that l1[1] and l2[1]...
"""

import math

lst = []
schedule = []
delLater = False

for i in range(3):                  #make list of numbers
    lst.append(i+1)

if len(lst) % 2 != 0:               #if num of items is odd, add 'DELETE'
    lst.append('DELETE')
    delLater = True


while len(schedule) < math.factorial(len(lst))/(2*math.factorial(len(lst) - 2)): #!(n)/!(n-k)

    mid = len(lst)/2

    l1 = lst[:mid]
    l2 = lst[mid:]

    for i in range(len(l1)):            
        schedule.append((l1[i], l2[i]))         #add lst items in schedule

    l1.insert(1, l2[0])             #rotate lst
    l2.append(l1[-1])
    lst = l1[:-1] + l2[1:]


if delLater == True:                #PROBLEM!!! One DELETE always left in list
    for x in schedule:
        if 'DELETE' in x:
            schedule.remove(x)

i = 1
for x in schedule:
    print i, x
    i+=1

4 个答案:

答案 0 :(得分:7)

schedule[:] = [x for x in schedule if 'DELETE' not in x]

查看有关deleting from a list while iterating over it的其他问题。

答案 1 :(得分:4)

要在迭代时从列表中删除元素,您需要倒退:

if delLater == True:
    for x in schedule[-1::-1]):
        if 'DELETE' in x:
            schedule.remove(x)

更好的选择是使用列表理解:

schedule[:] = [item for item in schedule if item != 'DELETE']

现在,您可以schedule =代替schedule[:] = - 有什么区别?一个例子是:

schedule = [some list stuff here]  # first time creating
modify_schedule(schedule, new_players='...') # does validation, etc.

def modify_schedule(sched, new_players):
    # validate, validate, hallucinate, illustrate...
    sched = [changes here]

此时,modify_schedule所做的所有更改都已丢失。为什么?因为它不是修改列表对象,而是将名称sched重新绑定到新列表,使原始列表仍然绑定到调用者中的名称schedule

因此,您是否使用list_object[:] =取决于您是否希望绑定到列表的任何其他名称来查看更改。

答案 2 :(得分:3)

迭代时不应修改列表:

for x in schedule:
    if 'DELETE' in x:
        schedule.remove(x)

相反,请尝试:

schedule[:] = [x in schedule where 'DELETE' not in x]

更多信息,请参阅Remove items from a list while iterating

答案 3 :(得分:2)

不要修改被迭代的序列。

schedule[:] = [x for x in schedule if 'DELETE' not in x]