prs11
prs11

Reputation: 27

Not able to find what is the syntax error in the code?

def makeInverseIndex(strlist):
 return { s:{ j if strlist[i] in strlist[j].split() for j in range(len(strlist)) } 
        for (i,s) in enumerate(strlist) }

What is the syntax error in the code in Python??

Upvotes: 0

Views: 86

Answers (1)

Jared
Jared

Reputation: 26407

You can't have an if statement to the left of for inside any comprehension unless you also have an else part (ternary operator). You need to move if strlist[i] in strlist[j].split() to the right,

def makeInverseIndex(strlist):
    return {s:{j for j in range(len(strlist)) if strlist[i] in strlist[j].split()}
            for (i,s) in enumerate(strlist)}

Upvotes: 5

Related Questions