All problems
Build an Inverted Index
Problem
Build an inverted index over docs, a list of token lists.
Return a dictionary mapping each distinct token to the sorted list of document indices that contain it. An empty corpus yields an empty dictionary.
Hint
enumerate gives the document id, and a set per document removes duplicate postings.
Test cases
case 1
in: [[["a","b"],["b","c"],["a","c"]]]
expected: {"b":[0,1],"a":[0,2],"c":[1,2]}
case 2
in: [[[],["x"]]]
expected: {"x":[1]}
case 3
in: [[]]
expected: {}
case 4
in: [[["the","cat"],["the","dog","cat"]]]
expected: {"cat":[0,1],"the":[0,1],"dog":[1]}
Starter codePython
def inverted_index(docs):
# Your code here
pass4 test cases · run your code against them in the editor