Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Common elements between two lists and preserving the order of elements in the two lists

Tags:

python

list

I have two lists list1 and list2. I've found on stackoverflow a very simple method to get the common elements in this two lists as follows result = list(set(list1) & set(list2)). Unfortunately, with this, the order of elements in the resulting list, is not preserved.

For instance:

list1 = ['a', 'e', 't', 'b', 'c']
list2 = ['e', 'b', 'a', 'c', 'n', 's']

I want the result (common elements) to be ['e', 'a', 'b', 'c'] in this order. Because, for instance, 'e' is in list1 and in list2 and is in position 2 in list1 and position 1 in list2, while 'a' is in list1 and in list2 and is in position 1 in list1 and position 3 in list2, so 'e' is before 'a' since 2+1 < 1+3.

So, is there any simple way to have the common elements between two lists and preserving the order of elements ?

like image 271
shn Avatar asked Jan 10 '14 13:01

shn


People also ask

How do you find the common element between two lists?

Method 1:Using Set's & property Convert the lists to sets and then print set1&set2. set1&set2 returns the common elements set, where set1 is the list1 and set2 is the list2. Below is the Python3 implementation of the above approach: Python3.

Do lists preserve the order of the elements they contain?

Yes, the order of elements in a python list is persistent.

How do you find the common element in a list?

Find the common items from two lists using set(). intersection() Example 3- Using set( ). intersection(), We can print the list of common elements of a list, but it can not store in any of the variables.

How to get common elements from two lists in Java?

Here we will see how to get common elements from two lists we can even say the intersection of two lists using different ways. retainAll () method from the Collection interface is used to remove the common elements from two different lists. We even get the common elements from two lists using java 8 stream API distinct () method.

How to find common elements present in two lists in Python?

In this article, we will understand how to find common elements present in two given lists if they exist, in Python using Python sets. We will be doing so by creating Python set objects using the given lists and then comparing them using the & operator and using the set intersection () method.

How to remove all common elements of two lists in Python?

Given two lists, the task is to write a Python program to remove all the common elements of two lists. Explanation: Lists after removing common elements of both the lists i.e, 4 and 5. between them. The remove () method removes the first matching element (which is passed as an argument) from the list.

What is the Order of the original color lists?

Original lists: ['red', 'green', 'black', 'orange'] ['red', 'pink', 'green', 'white', 'black'] ['white', 'orange', 'pink', 'black'] Test common elements between color1 and color2 are in same order? True Test common elements between color1 and color3 are in same order? False Test common elements between color2 and color3 are in same order?


2 Answers

You could use a list comprehension to filter all elements from list1 that don't also belong to list2:

list1 = ['a', 'e', 't', 'b', 'c']
list2 = ['e', 'b', 'a', 'c', 'n', 's']

result = [item for item in list1 if item in list2]
print result

Result:

['a', 'e', 'b', 'c']

Although this does not conform to the desired result in your main post, from your follow-up comment it seems like this is an acceptable result.


You could also continue using the set approach, and then sort the results after the fact using the positioning algorithm you described:

list1 = ['a', 'e', 't', 'b', 'c']
list2 = ['e', 'b', 'a', 'c', 'n', 's']

items = set(list1) & set(list2)
result = sorted(items, key=lambda element: list1.index(element) + list2.index(element))

print result

Result:

['e', 'a', 'b', 'c']
like image 52
Kevin Avatar answered Oct 27 '22 01:10

Kevin


list1 = ['a', 'e', 't', 'b', 'c']
list2 = ['e', 'b', 'a', 'c', 'n', 's']

weights = defaultdict(int)

for i, e in enumerate(list1):
   weights[e] += i

for i, e in enumerate(list2):
   weights[e] += i

>>> result = sorted(set(list1) & set(list2), key=lambda i: weights[i])
>>> result
['e', 'a', 'b', 'c']
like image 42
ndpu Avatar answered Oct 27 '22 01:10

ndpu