Find lists which intersect with one certain list [closed]

I have some problem with searching all lists within a big list that intersect (having at least one element in common) with one certain list.

In the attachment there are two text files.

First one: https://file.io/X7pxqSvVGsSC

Second one: https://file.io/GydGKHdZ7bao

After downloading it, the files should be read like this:

with open("list_big.txt", "r") as f:
    list_big = f.readlines()

with open("list_to_search_in.txt", "r") as f:
    list_to_search_in = f.readlines()

import json
for i in range(len(list_big)):
    list_big[i] = json.loads(list_big[i])

for i in range(len(list_to_search_in)):
    list_to_search_in[i] = json.loads(list_to_search_in[i])

After done this, there are 2 lists:

  • list_big is a large list consisting of lists. The inner lists have about 20 elements
  • list_to_search_in is a large list consisting of lists. But these inner lists have a fixed size of 3

Now what i have to do now using the example of the first inner list of list_big: Search all lists in list_to_search_in that contain at least one element of the first list of list_big.

And this is what i have to do for every list in list_big. What i've been trying to do so far:

res = [None]*len(list_big)
for i, list_b in enumerate(list_big):
    res[i] = []
    for list_s in list_to_search_in:
        if set(list_b) & set(list_s):
            res[i].append(list_s)

But this doesnt even come to an end. It takes way too long. I dont know if numpy or cython would help.

(note: using numpy-version: 1.12.1 --> cant be updated)



Read more here: https://stackoverflow.com/questions/65708789/find-lists-which-intersect-with-one-certain-list

Content Attribution

This content was originally published by Nik187 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: