0

I was trying with the below code and got the error:

def preprocess(s):
    return (word: True for word in s.lower().split())
s1 = 'This is a book'
text = preprocess(s1)

And then here comes the error that

return (word: True for word in s.lower().split()) 

is invalid syntax. I cannot find where the error comes from.

I want to put the sequence into this list model:

["This": True, "is" : True, "a" :True, "book": True]
jonrsharpe
  • 107,083
  • 22
  • 201
  • 376
Leo Li
  • 81
  • 7

2 Answers2

3

You want to construct a dictionary not a list. Use the curly braces { syntax instead:

def preprocess(s):
    return {word: True for word in s.lower().split()}
s1 = 'This is a book'
text = preprocess(s1)
sshashank124
  • 29,826
  • 8
  • 62
  • 75
0

What you want to do is place the sequence into a dictionary not a list. The format for a dictionary is:

dictionaryName={
    key:value,
    key1:value1,
}

So your code could work like this:

def preprocess(s):
    return {word:True for word in s.lower().split()}
s1 = 'This is a book'
text = preprocess(s1)
jonrsharpe
  • 107,083
  • 22
  • 201
  • 376
Dan
  • 507
  • 5
  • 16