如何从元组列表中删除行?

How to delete lines from list of tuples?

我有一个代码可以打印一个包含二元组的列表。对于进一步的话,我不需要包含某些单词的二元组。我有两个不同的停用词列表,我想将它们应用于双字母组。我想将一个列表应用于双字母组 (index[0]) 的第一个单词,并将一个列表应用于第二个单词 (index[1])。

我试过这样的事情:

if gram[0] in stop1 or gram[1] in stop2:
     print(gram)

现在打印了包含停用词的二元组,但如何从元组中删除这些行?

使用 for i in range 为您的搜索建立索引...

stopListA = [3,5]
myList = [(1,2), (3,4), (5,6)]
for i in range(len(myList)):
    if myList[i][0] in stopListA:
        myList[i] = myList[i][1]
print(myList)

[(1,2),4,6]

如果您不想打印包含停止列表中内容的元组,试试这个:

if gram[0] not in stop1 and gram[1] not in stop2:
     print(gram)