简体   繁体   中英

Finding common elements between 2 lists regardless of order- python 2

Id like to figure out a way to find all the similar elements from 2 different lists

The two list are as fallows,

A=[[[a1, b1],
  [a2, b2],
  [a3, b3]],
 [[a1, b2],
  [a2, b1],
  [a3, b3]],
 [[a1, b1],
  [a3, b2],
  [a2, b3]]]

B=[[[a1, b1],
  [a3, b3],
  [a2, b2]],
 [[a1, b2],
  [a2, b1],
  [a3, b3]],
 [[a1, b1],
  [a3, b2],
  [a2, b3]]]

Id consider the first elements of each list to be duplicates [[a1, b1],[a2, b2], [a3, b3]] and [[a1, b1],[a3, b3], [a2, b2]] to be duplicates.

The output Im looking for would be a list of either one of the common elements in a separate list as such,

C=[[a1, b1],[a2, b2], [a3, b3]]

Ive been working with this code, but it doesn't recognize they first two elements as duplicate and I'm wondering what I should add to account for it.

C=[ i for i in A if i in B]

For Unordered Unique Element Containers, Try Python Sets!

https://docs.python.org/3.8/library/stdtypes.html#set

Python Sets model mathematical sets in the sense that they are unordered containers of unique elements. It is not completely clear from your question exactly what you are considering to be duplicates. You may have to create sets of sets, but note than in order to have sets containing sets, you must use frozen_sets. The python documentation does a good job of describing these.

So your new code may look something like

A = set([frozen_set((a1,b1),
(a2. b2),
(a3, b3))
frozen_set((a1, b2),
(a1, b1),
(a3, b3))
... # And so on

NOTE: The internal lists were converted to tuples in order to be a hashable type

Again, the exact implementation of sets and frozen_sets depends on which elements you consider order and uniqueness to matter.

Set intersections

Once you have your set data structures correctly written, you can find all common elements with a set intersection.

C = A & B

It is equivalent to a mathematical set intersection.

A=[[["a1", "b1"],
  ["a2", "b2"],
  ["a3", "b3"]],
 [["a1", "b2"],
  ["a2", "b1"],
  ["a3", "b3"]],
 [["a1", "b1"],
  ["a3", "b2"],
  ["a2", "b3"]]]

B=[[["a1", "b1"],
  ["a3", "b3"],
  ["a2", "b2"]],
 [["a1", "b2"],
  ["a2", "b1"],
  ["a3", "b3"]],
 [["a1", "b1"],
  ["a3", "b2"],
  ["a2", "b4"]]]
ans=[]
for i in A:
    for j in B:
        for i1 in i:
            if i1 not in j:
                break
        else:
            ans.append(i)
            break
print ans

You can try something like this.

To compare for identical sublists regardless of order, one way is using zip and map by sorting each sublist first:

l1 = [[['a1', 'b1'], ['a2', 'b2'], ['a3', 'b3']],
      [['a1', 'b2'], ['a2', 'b1'], ['a3', 'b3']],
      [['a1', 'b1'], ['a3', 'b2'], ['a2', 'b3']]]
l2 = [[['a1', 'b1'], ['a3', 'b3'], ['a2', 'b2']],
      [['a1', 'b2'], ['a2', 'b1'], ['a3', 'b3']],
      [['a1', 'b1'], ['a3', 'b2'], ['a2', 'b4']]]

                              # sort the sublists first          # check if the sorted lists are equal        
result = [x for x, y in zip(*(map(sorted, l) for l in (l1, l2))) if x == y]

Identical Sublists Result:

[[['a1', 'b1'], ['a2', 'b2'], ['a3', 'b3']],
 [['a1', 'b2'], ['a2', 'b1'], ['a3', 'b3']]]

Update: Since you are using sympy and experiences an error, it's likely it happened on the sorted function per this question . Try this solution instead:

                                       # check if all items in x are also part of y
result = [x for x, y in zip(l1, l2) if all(subx in y for subx in x)]

If you are only concerned about getting the common elements within each sublist, here is another way:

          # check for common elements, put them in a list and return
result = [[sub_x for sub_x in x if sub_x in y] for x, y in zip(l1, l2)]

Common Elements Result:

[[['a1', 'b1'], ['a2', 'b2'], ['a3', 'b3']],
 [['a1', 'b2'], ['a2', 'b1'], ['a3', 'b3']],
 [['a1', 'b1'], ['a3', 'b2']]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM