Quick search for all items in two lists

Say I have two large lists: list_of_A_objects, which contain objects of class A, and a list of _of_B_objects, which contain objects of class B.

Both of them have line members.

I want to be able to search all the elements in two lists, and if the string member of object A is a substring of the string element of object B, I want it to do something.

What I have is fine if the lists are pretty small, but if the lists are big, it can take a lot of time.

Is there any way to do this faster. I was thinking about using dictionaries in some way because they have a quick search, but I can't figure it out.

This is what I still have.

class A:
    def __init__(self, x):
        self.string = x

class B:
    def __init__(self,x):
        self.string = x

list_of_A_objects = get_large_list_of_A_objects()

list_of_B_objects = get_large_list_of_B_objects() 


for A_object in list_of_A_objects:
    for B_Object in list_of_B_objects:
        if A_object.string in B_Object.string:
            do_something()
+4
2

, , B. , , . . .

, python, .

BStrings = ""
list_of_Indexes = new list of int
for B_object in list_of_B_objects
    list_of_Indexes.Add(length of BStrings)
    BStrings = BStrings + B_Object.string + newline

BStrings A_object. , , . list_of_indexes, , B_object .

( MxN, M - A, N - B), , B, .

, - Aho-Corasick. , , Python.

+2

python .

a_map = {}

for A_object in list_of_A_objects:
    a_map[A_object.string] = A_object

, ( ), do do_something

for B_object in list_of_B_objects:
    if B_object.string in a_map:
        do_something(a_map[B_object.string])

, A_ . , a_map .

0

All Articles