Python intersection of two duplicate lists

I have two flat lists in which one of them contains duplicate values. For instance,

array1 = [1,4,4,7,10,10,10,15,16,17,18,20] array2 = [4,6,7,8,9,10] 

I need to find the values ​​in array 1, which are also in array2, SAVING DUPLICATES in array 1. The desired result will be

 result = [4,4,7,10,10,10] 

I want to avoid loops as the actual arrays will contain more than a million values. I tried various combinations of sets and intersections, but just couldn't keep duplicates.

Any help would be greatly appreciated!

+7
source share
3 answers

What does it mean you don't want to use loops? You will have to sort it out anyway. Just take each element separately and check if it is in array2 when you go:

 items = set(array2) found = [i for i in array1 if i in items] 

Also, depending on how you intend to use the result, consider having a generator:

 found = (i for i in array1 if i in array2) 

so that you do not need all this in memory at once.

+5
source

After that do the following:

 array1 = [1,4,4,7,10,10,10,15,16,17,18,20] array2 = [4,6,7,8,9,10] set2 = set(array2) print [el for el in array1 if el in set2] 

It preserves the order and repetition of elements in array1 .

It turns array2 into a set for faster searches. Note that this is only useful when array2 is large enough; if array2 small, it may be more efficient to save it as a list.

+3
source

Based on @Alex's answer, if you also want to extract indexes for each token, then here's how:

 found = [[index,i] for index,i in enumerate(array1) if i in array2] 
0
source

Source: https://habr.com/ru/post/1205900/


All Articles