1

I wrote a code in python but I am not sure why it never get match while it should be able, would you mind check what is the issue in my code?

my assumption is my select query creating list of list "('bad' , ), ('worse' , ), but I am not sure how to remove it " ,"

I am using visual studio and python 3.5:

 if len(list(set(words) & set(badlst))) > 0:
        index +=1

words :(data details) ['term', 'saving', 'insurance', 'cost', '?', 'upgrade', 'plan', 'always', 'preferred', 'client', ',', 'bad', 'agent', 'explained', ...]

badlst : (data details) [('bad',), ('worse',), ('incorrigible',), ('poor',), ('dreadful',), ('atrocious',), ('cheap',), ('unacceptable',), ('sad',), ('lousy',), ('crummy',), ('awful',), ('rough',), ('synthetic',), ...]

I generate badlst as below:

def readInsCmp(column, tablename, whereCondition):
    command = "select "+ column+" from "+ tablename +" where "+ whereCondition 
    c.execute(command)
    namelist = c.fetchall()
    return namelist

badlst = readInsCmp("distinct(word)","wordVals","value=-1")

Words parameter is based on parse some input from excel file:

sentenceArr = sent_tokenize(content)
for sentence in sentenceArr:
 words = [word for word in nltk.word_tokenize(sentence) if word not in stopwords.words('english')]

1 Answer 1

3

If I have interpreted the question correctly, you have two lists:

>>> words = ['term', 'saving', 'insurance', 'cost', '?', 'upgrade', 'plan', 'always', 'preferred', 'client', ',', 'bad', 'agent', 'explained', ]
>>> badlst = [('bad',), ('worse',), ('incorrigible',), ('poor',), ('dreadful',), ('atrocious',), ('cheap',), ('unacceptable',), ('sad',), ('lousy',), ('crummy',), ('awful',), ('rough',), ('synthetic',), ]

And, you want to find if the two lists have any words in common. If so, the first thing to do is to convert badlst from a list of tuples to a list of words:

>>> badlst2 = [x[0] for x in badlst]

With that done, it is easy to find words in common:

>>> set(words).intersection(badlst2)
{'bad'}

We can use that intersection in an if statement as follows:

>>> index = 0
>>> if set(words).intersection(badlst2):
...     index += 1
... 
>>> index
1

The if statement works because, as per python convention, the boolean value of a set is False if it is empty and True otherwise.

As an alternative to an if statement, we can add the boolean value of the set to index:

>>> index = 0
>>> index += bool( set(words).intersection(badlst2) )
>>> index
1

Aside

In python, & performs a bitwise-and, not an intersection. This is not what you want.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.