代码之家  ›  专栏  ›  技术社区  ›  fugu

循环期间从列表列表中移除元素

  •  -1
  • fugu  · 技术社区  · 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 回复  |  直到 7 年前
        1
  •  1
  •   MA1    7 年前

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

    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])
    
        2
  •  1
  •   ankit agrahari    7 年前

    您可以使用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
    
        3
  •  0
  •   Sreyas    7 年前

    复制列表并遍历副本并从原始目录中删除该项。

    for i in info[:]:
      info.remove(i)
    
        4
  •  0
  •   Ma0    7 年前

    先清理输入,然后循环:

    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']
    

    现在你可以像以前那样继续了。