hahahj
hahahj

Reputation: 1

How to compare list of lists in Python?

I have 2 lists like the following:

a=[[1,0,1,0,1],[0,0,0,1,0],[1,1,0,0,0]]
b=[[1,0,0,0,1],[0,1,0,1,0],[1,1,0,1,0]]

I want to return true if all the sublists in b are present in a and vice versa. That means a should be equal to b but indexes of the sublists can be different. eg:

a=[[1,0,1,0,1],[0,0,0,1,0],[1,1,0,0,0]]
b=[[1,0,1,0,1],[1,1,0,0,0],[0,0,0,1,0]]

Above a and b are equal and comparison should return true. Also, the sublists will only contain a combination of 1s or 0s. How do I compare them? I tried converting them to sets : set(a) but this is throwing an error. Apart from that, when I tried the following code in a while loop, it gave an error

a=[[1,0,1,0,1],[0,0,0,1,0],[1,1,0,0,0]]
b=[[1,0,1,0,1],[1,1,0,0,0],[0,0,0,1,0]]

def sublists_equal(a, b):
    return all(l for l in b if l in a)

print(sublists_equal(a, b))

The error was:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

I tried printing both the arrays to see what the problem was, they are printing like follows:

[[0 1 0 1 0]
 [0 1 1 1 1]
 [0 0 0 0 1]
 [0 1 0 0 0]]
[array([0, 1, 0, 1, 0]), array([0, 0, 0, 0, 1]), array([0, 1, 1, 1, 1]), array([0, 1, 0, 0, 0])]

Upvotes: 0

Views: 2636

Answers (3)

Bart Gras
Bart Gras

Reputation: 1

Sorry for this late response. I came across this topic in search of some suggestions about a similar problem. You can solve it by using (based on answer solid.py, but I am a newbie and cannot add comments yet):

a=[[1,0,1,0,1],[0,0,0,1,0],[1,1,0,0,0]]
b=[[1,0,1,0,1],[1,1,0,0,0],[0,0,0,1,0]]
c=[[1,0,1,0,1],[1,1,0,0,0],[0,0,0,1,1]]

def sublists_equal(a, b):
    return all(l in a for l in b)

print(sublists_equal(a, b))
print(sublists_equal(a, c))

Upvotes: 0

thebjorn
thebjorn

Reputation: 27311

The easiest way is to sort the lists before comparing them:

def compare_lists(a, b):
    return sorted(a) == sorted(b)

this handles cases where there are duplicate values.

If duplicates don't matter, and the sublists only contains 1 and 0, you could convert them to integers:

def sublists_to_integers(lst):
    return [int(''.join(str(i) for i in item), 2) for item in lst]

def compare_lists(a, b):
    return set(sublists_to_integers(a)) == set(sublists_to_integers(b))

which depending on your list size might be faster and/or use less memory.

Upvotes: 0

solid.py
solid.py

Reputation: 2812

You could use the all() built-in function to check whether all sub-lists l of b can be found in a.

a=[[1,0,1,0,1],[0,0,0,1,0],[1,1,0,0,0]]
b=[[1,0,1,0,1],[1,1,0,0,0],[0,0,0,1,0]]

def sublists_equal(a, b):
    return all(l for l in b if l in a)

print(sublists_equal(a, b))

Should you wanted to use sets, you would have to convert each sub-list to a tuple, which is a hashable type, and then compare their symmetric difference denoted by the ^ operator which returns the set of elements not found in both lists. If the symmetric difference is the empty set(), which is negated to True using the not operator, then the lists have equal sub-lists.

def sublist_equal2(a, b):
    return not set([tuple(l) for l in a]) ^ set([tuple(l) for l in b])

print(sublist_equal2(a, b))

Output:

True

Upvotes: 2

Related Questions