How to efficiently find the indices of matching elements in two lists
问题 I am working on two large data sets, and my question is as follows. Suppose I have two lists: list1 = [A,B,C,D] list2 = [B,D,A,G] How can I efficiently find the matching index, using Python, other than O(n 2 ) searching? The result should look like: matching_index(list1,list2) -> [(0,2),(1,0),(3,1)] 回答1: Without duplicates If your objects are hashable and your lists have no duplicates, you can create an inverted index of the first list and then traverse the second list. This traverses each