What I want is get two dictionaries based on a sorted list
The form of input list is (string, id). Example: [("car", 1), ("car", 2), ("car", 2), ("book", 1), ("banana", 3)]
The form of first output dictionary is (id, term_frequency). Example: {"car": [(1, 1), (2, 2)], "book": [(1, 1)], "banana": [(3, 1)]}
The form of second output dictionary is the unique term frequency. Example: {"car": 2, "book": 1, "banana": 1} as there are two unique terms of car, car 1 and car 2
However, I only finished part of the code and can't continue, I can't get the logical stright. As the comment described, I do have a problem on update the dictionary. It seems that I should keep iterating through the list and check the conditions, maybe an if loop?
I wonder how should I keep working from my current code.
input_list = [("car", 1), ("car", 2), ("car", 2), ("book", 1), ("banana", 3)]
def get_index(list)
index = {}
doc_freq = {}
index = 0
while(index < len(list)):
(term, doc_id) = list[index]
index[term] = [(doc_id, 1)]
#here is where I can't continue
for(term, value) in index.items():
doc_freq[term] = len(value)
return index, doc_freq