Approve list equality list without order in Python

What is the best way to claim two lists of lists are equal without order value? for example, these two lists are equal:

a = [[1,2], [3,4]]
b = [[4,3], [2,1]]

assert lists_equal_without_order(a, b)  # True

How can one implement lists_equal_without_order, ideally, using some existing Python statements?

+4
source share
6 answers

If the elements in aor bare no duplicates in the element, you can use a variety of concepts for collecting frozen set each item. For instance,

In [106]: {(frozenset(item)) for item in a}
Out[106]: {frozenset({1, 2}), frozenset({3, 4})}

Then check if these sets are equal:

In [107]: {(frozenset(item)) for item in a} == {(frozenset(item)) for item in b}
Out[107]: True

, , frozensets - (, , ). , frozenset [1,2] [2,1]:

In [109]: frozenset([1,2]) == frozenset([2,1])
Out[109]: True

, , frozensets [1,1,2] [2,2,1]:

In [108]: frozenset([1,1,2]) == frozenset([1,2,2])
Out[108]: True
+1

, :

def lists_equal_without_order(a, b):
    """
    We don't care about duplicates in list comparison or the sublist comparison.
    * [1,2,2] === [1,1,2]  # True
    * [[1,1], [1,1]] == [[1,1]] # True
    The element lists are either the same length or we don't care about duplicates
    * [1,1,1] === [1]  # True
    """
    for l1 in a:
        check_list = frozenset(l1)
        if not any(check_list.issuperset(l2) for l2 in b):
            return False
    return True

a = [[1,2], [3,4]]
b = [[4,3], [2,1]]

print lists_equal_without_order(a, b)  # True

a = [[1,1], [2,2]]
b = [[1,2], [2,1]]

print lists_equal_without_order(a, b)  # False

, :

def lists_equal_without_order(a, b):
    """
    This will manipulate the original lists.
    """
    for l1 in a:
        l1.sort()
        for l2 in b:
            l2.sort()
            if l1 == l2:
                b.remove(l2)
                break
        else:
            return False
    return True

a = [[1,2], [3,4]]
b = [[4,3], [2,1]]

print lists_equal_without_order(a, b)  # True

a = [[1,1], [2,2]]
b = [[1,2], [2,1]]

print lists_equal_without_order(a, b)  # False

, , 2 :

from collections import Counter

def lists_equal_without_order(a, b):
    """
    This will make sure the inner list contain the same, 
    but doesn't account for duplicate groups.
    """
    for l1 in a:
        check_counter = Counter(l1)
        if not any(Counter(l2) == check_counter for l2 in b):
            return False
    return True

a = [[1,2], [3,4]]
b = [[4,3], [2,1]]

print lists_equal_without_order(a, b)  # True

a = [[1,1], [2,2]]
b = [[1,2], [2,1]]

print lists_equal_without_order(a, b)  # False
+2

, , .

-

def lewo(l1, l2):
    l1new = [sorted(i) for i in l1]
    l2new = [sorted(i) for i in l2]
    l1newsorted = sorted(l1new)
    l2newsorted = sorted(l2new)
    return l1newsorted == l2newsorted

-

def lewo(a, b):
    a_s, b_s = map(sorted, a), map(sorted, b)
    return sorted(a_s) == sorted(b_s)
+1

( ), :

def lists_equal_without_order(a, b):
    return set_repr(a) == set_repr(b)

def set_repr(x):
    return {frozenset(item) for item in x}

, :

from collections import Counter

def lists_equal_without_order(a, b):
    return counter_repr(a) == counter_repr(b)

def counter_repr(x):
    return {frozenset(Counter(item).items()) for item in x}

If the sublists themselves can occur several times (i.e. if the external list contains duplicates), we can use the counter for the external list:

from collections import Counter

def lists_equal_without_order(a, b):
    return counter_repr(a) == counter_repr(b)

def counter_repr(x):
    return Counter(frozenset(Counter(item).items()) for item in x)
+1
source

Run a loop well for the size of the list.

A = list a;

B = list b;

For i running from 0: loop size

{Temp = a [i];

If (temp exists in set B) Remove the tempo from B;

}

If B is empty, then A = B;

Else A! = B p>

0
source

another approach set/frozenset:

a = [[1,2], [3,4]]
b = [[4,3], [2,1]]

def lists_equal_without_order(lst0, lst1):
    set0 = set( frozenset(item) for item in lst0 )
    set1 = set( frozenset(item) for item in lst1 )
    return set0 == set1

print(lists_equal_without_order(a,b))

I'm not quite sure that it covers all your use cases. works for the example you gave though ...

0
source

Source: https://habr.com/ru/post/1598585/


All Articles