gpt4 book ai didi

Python:搜索元组列表,删除整个索引

转载 作者:行者123 更新时间:2023-11-28 23:00:41 25 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Remove items from a list while iterating in Python

我有一个相当嵌入式的列表:具体来说,它是一个元组列表的列表。为了简化事情,整个列表是一个句子列表。在每个句子中,每个单词都构成一个元组,其中包含有关该单词的信息。每个句子中的最后一个元组包含有关说话者的信息,但如果需要,可以将其删除。

我想搜索这些元组,如果找到某个值,则删除整个句子。

这是一个示例列表:

sentenceList = [[('the', 'det', '1|2|DET'), ('duck', 'n', '2|3|SUBJ'), ('xxx', 'unk', '3|0|ROOT'), ('*MOT', 373)],
[('yyy', 'unk', '1|0|ROOT'), ('*CHI', 375)],
[('what', 'pro', '1|2|OBJ'), ('happen-PAST', 'v', '2|0|ROOT'), ('to', 'prep', '3|2|JCT'), ('the', 'det', '4|5|DET'), ('duck', 'n', '5|3|POBJ'), ('*MOT', 378)],
[('boom', 'int', '1|0|ROOT'), ('*CHI', 379)]]

如果一个句子包含 'xxx''yyy',我想删除整个句子。我试过的代码是:

wordList = ['xxx','yyy']
for sentence in sentenceList:
for wordTuple in sentence:
for entry in wordTuple:
if entry in wordList:
del sentence

这应该删除整个句子,即:

[('the', 'det', '1|2|DET'), ('duck', 'n', '2|3|SUBJ'), ('xxx', 'unk', '3|0|ROOT'), ('*MOT', 373)], [('yyy', 'unk', '1|0|ROOT'), ('*CHI', 375)]

但是,这段代码似乎并没有完成任务。知道如何解决吗?谢谢!

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com