I’m working using Python.
I have a query, with words. For example, query=[hello, tree, blue]
I’ve selected for each word in which documents it is, so I have a list for each word, where each position is one of the documents. Let’s say:
list_query[0]=[1,4,5]
list_query[1]=[5,8]
list_query[2]=[4,5,8]
So, I should get a result = [5]
But, I don’t wanna do it using intersection. I need to do it using iterations, i, j.
hello:
i
|
1 4 5
tree:
5 8
|
j
I’ll have to start with i=0, compare if the list_query[0][i]==list_query[1][j], if so add that number to the list. If not I should iterate the smaller number of boths iterators, and so on, with the result of that intersection of those lists and the rest of the elements of the query. But kind find how to do it and it’s driving me mad.
So if anyone could help me…
Thanks in advance.
I feel that you’re already pretty far along. I could show you an implementation, but you’ve described the algorithm already, so implementing it yourself shouldn’t be hard. But perhaps you don’t feel confident in your description.
Let me restate your description in a slow and precise way, leaving out the information we don’t need about queries and such. We have two pre-sorted lists, and we want to find their intersection. Adapting your diagram with a slightly fuller example, starting with list
a = [1, 4, 5, 7, 8],b = [5, 8, 9],i=0, andj=0, as well as an empty output listout = []which I’ll leave out of the diagram initially…First we check if they’re equal. They aren’t, so we take the minimum of
a[i]andb[j]. In this case,a[i] == 1andb[j] == 5, so we want to incrementi.Going through the same steps, we increment
iagain:Now things go differently;
a[i]andb[j]are the same, so we want to append that value to the output list and increment both values:Now we continue.
a[i]is again less thanb[j]…And the values are the same, so we add that value to
outand incrementiandj…But now we find that
i == len(a). So we know the algorithm has terminated.Now we have all we need to establish what variables we need and how the logic should work. We need list
a, listb, indexi, indexj, and listout. We want to create a loop that stops when eitheri == len(a)orj == len(b), and within that loop, we want to testa[i]for equality withb[j]. If they are equal, we increment bothiandjand appenda[i]toout. If they are not equal, then we test whethera[i] < b[j]. If it is, then we incrementi; otherwise, we incrementj.This determines the intersection between two lists; now we just have to apply this to the first and the second list, and then apply the result to the third list, and then apply the result of that to the fourth, and so on.