2

I tried to used 'in' function to check some value available in a list or not. But it does not work correctly.

from nltk.tokenize import word_tokenize

x ='what are bad'
a= word_tokenize(x)[0]
qestion = ['what,why,how,are,am,should']

if a in qestion:
 print 'question'
else:
 print 'not a question'

I couldn't find the reason for that. Can any one help me to solve this.

1
  • 1
    I don't think ['what,why,how,are,am,should'] does what you want: that's a list with only 1 element in it Commented Jun 3, 2017 at 5:25

1 Answer 1

6

The following certainly is a list, but I don't think it's the list that you were looking for.

 qestion = ['what,why,how,are,am,should']

i think what you wanted was

question = 'what,why,how,are,am,should'.split(',')

which produces

['what', 'why', 'how', 'are', 'am', 'should']

Which is more likely to full fill your IF x in y condition.

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

4 Comments

Thank you. This is what I expected.
Glad to have been of help and all the best with your project.
Can you explain please why we should use .split(',') in this case.
Well the split function breaks up a strin into pieces. Since laziness is a virtue in programming using split here is easier than manually converting your original list to the required format :-)

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.