如何比较列表中无序列表的两个无序列表?

时间:2017-10-06 13:13:20

标签: python list comparison

如何比较列表中无序列表的两个无序列表?

假设两个列表列表中的列表不一定是相同的顺序。还假设列表列表中给定列表中的项目顺序不一定是相同的顺序。示例如下所示:

dummy_list_A =  [['A'], ['B'], ['C', 'D']]
dummy_list_B =  [['B'], ['A'], ['D', 'C']]

我已经查看了Stack Overflow以获取Test if two lists of lists are equal等问题的答案,但这些问题都没有奏效。您可以在下面看到,比较列表列表的最常见答案对我的方案不起作用。

print sorted(dummy_list_A) == sorted(dummy_list_B)
False

print set(map(tuple,dummy_list_A)) == set(map(tuple,dummy_list_B))
False

print ((len(dummy_list_A) == len(dummy_list_B)) and (all(i in dummy_list_B for i in dummy_list_A)))
False

6 个答案:

答案 0 :(得分:3)

首先,您需要对子列表列表的列表元素进行排序,然后对主列表进行排序

org.sonatype.nexus.repository.storage.RetryDeniedException: Reached max retries: 8/8
6.10.2017 15:17:14  at org.sonatype.nexus.repository.storage.StorageTxImpl.allowRetry(StorageTxImpl.java:216)
6.10.2017 15:17:14  at sun.reflect.GeneratedMethodAccessor342.invoke(Unknown Source)
6.10.2017 15:17:14  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
6.10.2017 15:17:14  at java.lang.reflect.Method.invoke(Method.java:498)
6.10.2017 15:17:14  at org.sonatype.nexus.common.stateguard.SimpleMethodInvocation.proceed(SimpleMethodInvocation.java:53)
6.10.2017 15:17:14  at org.sonatype.nexus.common.stateguard.StateGuardAspect$1.invoke(StateGuardAspect.java:69)
6.10.2017 15:17:14  at com.sun.proxy.$Proxy226.allowRetry(Unknown Sour
 .
 .
 .
6.10.2017 15:17:14Caused by: org.sonatype.nexus.repository.storage.MissingBlobException: Blob default@1E8F08E5-2C4760ED-7456B67F-5BA48EFB-5A2C18C3:abfa2c22-259a-49bd-85de-4d0431dc0174 exists in metadata, but is missing from the blobstore
6.10.2017 15:17:14  at org.sonatype.nexus.repository.storage.StorageTxImpl.requireBlob(StorageTxImpl.java:847)
6.10.2017 15:17:14  at sun.reflect.GeneratedMethodAccessor252.invoke(Unknown Source)
6.10.2017 15:17:14  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
6.10.2017 15:17:14  at java.lang.reflect.Method.invoke(Method.java:498)
6.10.2017 15:17:14  at org.sonatype.nexus.common.stateguard.SimpleMethodInvocation.proceed(SimpleMethodInvocation.java:53)
6.10.2017 15:17:14  at org.sonatype.nexus.common.stateguard.MethodInvocationAction.run(MethodInvocationAction.java:39)
6.10.2017 15:17:14  at org.sonatype.nexus.common.stateguard.StateGuard$GuardImpl.run(StateGuard.java:270)
6.10.2017 15:17:14  at org.sonatype.nexus.common.stateguard.GuardedInterceptor.invoke(GuardedInterceptor.java:53)
6.10.2017 15:17:14  at org.sonatype.nexus.common.stateguard.StateGuardAspect$1.invoke(StateGuardAspect.java:63)
6.10.2017 15:17:14  at com.sun.proxy.$Proxy226.requireBlob(Unknown Source)
6.10.2017 15:17:14  at org.sonatype.nexus.repository.docker.internal.DockerHostedFacetImpl.getManifestByTag(DockerHostedFacetImpl.java:745)
6.10.2017 15:17:14  at org.sonatype.nexus.transaction.TransactionalWrapper.proceedWithTransaction(TransactionalWrapper.java:56)
6.10.2017 15:17:14  ... 147 common frames omitted

答案 1 :(得分:0)

您可以简单地对子列表中的元素进行排序,对子列表进行排序并比较两个列表。

>>> dummy_list_A =  [['A'], ['B'], ['C', 'D']]
>>> dummy_list_B =  [['B'], ['A'], ['D', 'C']]
>>> sorted(map(sorted, dummy_list_A))
[['A'], ['B'], ['C', 'D']]
>>> sorted(map(sorted, dummy_list_B))
[['A'], ['B'], ['C', 'D']]
>>> def signature(l):
...   return sorted(map(sorted, l))
... 
>>> signature(dummy_list_A) == signature(dummy_list_B)
True

答案 2 :(得分:0)

dummy_list_A =  [['A'], ['B'], ['C', 'D']]
dummy_list_B =  [['B'], ['A'], ['D', 'C']]
print( sorted(sum(dummy_list_A,[])) == sorted(sum(dummy_list_B,[])) )
  

答案 3 :(得分:0)

您可以使用Python的.sort()方法对内部列表和列表列表进行排序。

  list_1 = [['A'], ['B'], ['C', 'D']]
  list_2 = [['B'], ['A'], ['D', 'C']]

   def list_sorter(list_of_list):
       for a_list in list_of_list:
           a_list.sort()
       list_of_list.sort()
   list_sorter(list_1)
   list_sorter(list_2)
   print(list_1 == list_2)
   True

答案 4 :(得分:0)

我将如何解决它。

sorted(dummy_list_B, key=lambda x: sorted(x)) == sorted(dummy_list_A, key=lambda x: sorted(x))

答案 5 :(得分:0)

您可以将列表列表转换为冻结集集。然后,您可以比较两组:

dummy_list_A =  [['A'], ['B'], ['C', 'D']]
dummy_list_B =  [['B'], ['A'], ['D', 'C']]

set_A = {frozenset(i) for i in dummy_list_A}
set_B = {frozenset(i) for i in dummy_list_B}

set_A == set_B

您还可以执行其他set操作:

dummy_list_C =  [['B'], ['A'], ['D', 'E']]
set_C = {frozenset(i) for i in dummy_list_C}

set_A == set_C

错误

set_A.intersection(set_C)

{frozenset({'A'}),frozenset({'B'})}

set_A.difference(set_C)

{frozenset({'D','C'})}}

注意::如果外部列表多次包含相同的内部列表,则重复的列表将被缩减为一个冻结集,因此以下列表也将被视为相等:

dummy_list_A =  [['A'], ['A'], ['B'], ['C', 'D'], ['D', 'C']]
dummy_list_B =  [['B'], ['A'], ['D', 'C']]

set_A = {frozenset(i) for i in dummy_list_A}
set_B = {frozenset(i) for i in dummy_list_B}

set_A == set_B