S.H
S.H

Reputation: 61

How to delete lines from list of tuples?

I have a code that prints a list with tuples with bigrams. For further word, I don't need bigrams that contain certain words. I have two different stop word lists that I want to apply to the bigrams. I want to apply one list to the first words of the bigrams (index[0]) and one list to the second words (index[1]).

I tried something like this:

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

Now the bigrams that DO contain the stop words are printed, but how can I remove these lines from the tuple?

Upvotes: 1

Views: 148

Answers (2)

akhilsp
akhilsp

Reputation: 1103

If you don't want to print a tuple which has contents present in the stop-list, try this:

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

Upvotes: 2

J'e
J'e

Reputation: 3716

indexing your seach with a 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]

Upvotes: 1

Related Questions