在循环期间从列表列表中删除元素

时间:2018-07-19 09:07:24

标签: python python-2.7

我具有如下构造的数据结构info(列表的列表?):

pages     = [12, 41, 50, 111, 1021, 121]
bookCodes = ['M', 'P', 'A', 'C', 'A', 'M']
sentences = ['THISISASENTENCE',
             'ANDHEREISONEMOREEXAMP',
             'ALLFROMDIFFERENTBOOKS',
             'ANDFROMDIFFERENTPAGES',
             'MOSLTYTHESAMELENGTHS',
             'THISISSHORT'
             ]
info = list(zip(bookCodes, pages, sentences))

然后我一次遍历该列表(在压缩列表中)。由于某些句子比其他句子短(例如THISISSHORT),因此我在其他句子之前到达了最后一个元素。在这种情况下,我想完全屏蔽以后的迭代中的该元素,但是我不想在开始循环之前过滤它

import random

letters_read = 0

for i in range(21):
    random.shuffle(info)
    for b, p, s in info:
        if len(s) <= i+1:
            print("End of sentence reached at position %s. Sentence: %s" % (i, s))
            continue    
        letters_read += 1

我目前正在使用continue来跳过这种情况下的元素,并打印一条消息以指示该句子已经到达结尾。但是,这将继续在此元素上进行迭代,直到循环结束。 我想从进一步的迭代中排除此类元素。

我的列表中有〜10,000个句子,这些句子可能多达2000个字符,因此我假设通过从以后的迭代中屏蔽此类句子(而不是跳过)将提高我的剧本

是否可以在迭代过程中从此数据结构中删除/屏蔽元素?我尝试使用info.remove(i)del info[i],但这不起作用(因为这不是列表)。


输出

End of sentence reached at position 10. Sentence: THISISSHORT
End of sentence reached at position 11. Sentence: THISISSHORT
End of sentence reached at position 12. Sentence: THISISSHORT
End of sentence reached at position 13. Sentence: THISISSHORT
End of sentence reached at position 14. Sentence: THISISASENTENCE
End of sentence reached at position 14. Sentence: THISISSHORT
End of sentence reached at position 15. Sentence: THISISSHORT
End of sentence reached at position 15. Sentence: THISISASENTENCE
End of sentence reached at position 16. Sentence: THISISASENTENCE
End of sentence reached at position 16. Sentence: THISISSHORT
End of sentence reached at position 17. Sentence: THISISASENTENCE
End of sentence reached at position 17. Sentence: THISISSHORT
End of sentence reached at position 18. Sentence: THISISASENTENCE
End of sentence reached at position 18. Sentence: THISISSHORT
End of sentence reached at position 19. Sentence: THISISASENTENCE
End of sentence reached at position 19. Sentence: MOSLTYTHESAMELENGTHS

所需的输出


End of sentence reached at position 10. Sentence: THISISSHORT
End of sentence reached at position 14. Sentence: THISISASENTENCE
End of sentence reached at position 19. Sentence: MOSLTYTHESAMELENGTHS

4 个答案:

答案 0 :(得分:1)

您需要复制原始列表,然后遍历新副本并从原始列表中删除项目

for item in list(original_list):
  ...
  original_list.remove(item)

您的情况下,代码如下所示

total_read = 0

for i in range(21):
    random.shuffle(info)
    for index, value in enumerate(list(info)):
        b, p, s = value
        if len(s) <= i+1:
            print("Overshot! Shouldn't see this sentence anymore: %s" % (s))
            info.pop(index)
        print s[:i+1], i, s

        total_read += len(s[i + 1])

答案 1 :(得分:1)

您可以使用del从压缩列表中删除项目

for i in range(21):
    q=0
    for p, b, s in info:
        if len(s)<=i+1:
            print(f'Overshot! Remove this element {s} {q}')
            del info[q]
        print(f"{s[:i+1]}, {i}, {s}")
        total_read += len(s[i+1])
        q+=1

答案 2 :(得分:0)

先清理输入内容,然后遍历它:

sentences = ['THISISASENTENCE',
             'ANDHEREISONEMOREEXAMP',
             'ALLFROMDIFFERENTBOOKS',
             'ANDFROMDIFFERENTPAGES',
             'MOSLTYTHESAMELENGTHS',
             'THISISSHORT'
             ]
max_len = len(max(sentences, key=len))  # kudos ZachGates
print max_len  # 21
sentences = filter(lambda x: len(x)==max_len, sentences)
print sentences  # ['ANDHEREISONEMOREEXAMP', 'ALLFROMDIFFERENTBOOKS', 'ANDFROMDIFFERENTPAGES']

现在您可以像以前一样继续进行。

答案 3 :(得分:0)

制作列表的副本并遍历该副本,然后从原始列表中删除该项目。

for i in info[:]:
  info.remove(i)