简体   繁体   English

无论顺序如何,在 2 个列表之间查找共同元素 - python 2

[英]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我想找出一种从 2 个不同列表中找到所有相似元素的方法

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.我认为每个列表的第一个元素是重复的[[a1, b1],[a2, b2], [a3, b3]][[a1, b1],[a3, b3], [a2, b2]]要重复。

The output Im looking for would be a list of either one of the common elements in a separate list as such,我正在寻找的 output 将是单独列表中任一常见元素的列表,

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!对于无序的唯一元素容器,请尝试 Python 集!

https://docs.python.org/3.8/library/stdtypes.html#set 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. Python 集合 model 数学集合,因为它们是唯一元素的无序容器。 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.您可能必须创建集合集,但请注意,为了使集合包含集合,您必须使用 freeze_sets。 The python documentation does a good job of describing these. python 文档很好地描述了这些。

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.同样,sets 和 freeze_sets 的确切实现取决于您认为哪些元素的顺序和唯一性很重要。

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:为了比较相同的子列表而不考虑顺序,一种方法是使用zipmap ,首先对每个子列表进行排序:

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 .更新:由于您正在使用sympy并遇到错误,因此它很可能发生在按此问题sorted的 function上。 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']]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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