2

How do I compare two unordered lists of lists with unordered lists inside of them?

Assume that the lists within the two lists of lists are not necessarily in the same order. Also assume that the order of items in a given lists within the lists of lists are not necessarily in the same order. An example is shown below:

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

I have already looked on Stack Overflow for answers to questions such as Test if two lists of lists are equal and none of them worked. You can see for yourself below that the most common answers to comparing lists of lists does not work for my scenario.

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 Answers6

3

First you need to sort list elements of sub-list list and then main list

>>> sorted([sorted(i) for i in dummy_list_A])
[['A'], ['B'], ['C', 'D']]

>>> sorted([sorted(i) for i in dummy_list_B])
[['A'], ['B'], ['C', 'D']]

>>> sorted([sorted(i) for i in dummy_list_A]) == sorted([sorted(i) for i in dummy_list_B])
True
akash karothiya
  • 5,736
  • 1
  • 19
  • 29
0

You could simply sort the elements in the sublists, sort the sublists and compare the two lists.

>>> 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
Eric Duminil
  • 52,989
  • 9
  • 71
  • 124
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,[])) )

True

Bhupen
  • 1,270
  • 1
  • 12
  • 27
0

You can sort both the internal lists and the list of lists using Python's .sort() method.

  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
jmconrad
  • 1
  • 1
0

This how I would solve it.

sorted(dummy_list_B, key=lambda x: sorted(x)) == sorted(dummy_list_A, key=lambda x: sorted(x))
theBuzzyCoder
  • 2,652
  • 2
  • 31
  • 26
0

You can convert your lists of lists to sets of frozensets. You can then compare the two sets:

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

True

You can also perform other set operations:

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

set_A == set_C

False

set_A.intersection(set_C)

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

set_A.difference(set_C)

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

NOTE: If your outer list contains the same inner list multiple times, the duplicated lists will be reduced to one frozenset, so the following lists will also be considered equal:

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

True

ElArk
  • 23
  • 5