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
  • 115,751
  • 26
  • 228
  • 437
Leo Li
  • 81
  • 1
  • 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
  • 31,495
  • 9
  • 67
  • 76
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
  • 115,751
  • 26
  • 228
  • 437
Dan
  • 527
  • 4
  • 16