Reputation: 27
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
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