Check if two unordered lists are equal
Question:
I’m looking for an easy (and quick) way to determine if two unordered lists contain the same elements:
For example:
['one', 'two', 'three'] == ['one', 'two', 'three'] : true
['one', 'two', 'three'] == ['one', 'three', 'two'] : true
['one', 'two', 'three'] == ['one', 'two', 'three', 'three'] : false
['one', 'two', 'three'] == ['one', 'two', 'three', 'four'] : false
['one', 'two', 'three'] == ['one', 'two', 'four'] : false
['one', 'two', 'three'] == ['one'] : false
I’m hoping to do this without using a map.
Answers:
Python has a built-in datatype for an unordered collection of (hashable) things, called a set
. If you convert both lists to sets, the comparison will be unordered.
set(x) == set(y)
EDIT: @mdwhatcott points out that you want to check for duplicates. set
ignores these, so you need a similar data structure that also keeps track of the number of items in each list. This is called a multiset; the best approximation in the standard library is a collections.Counter
:
>>> import collections
>>> compare = lambda x, y: collections.Counter(x) == collections.Counter(y)
>>>
>>> compare([1,2,3], [1,2,3,3])
False
>>> compare([1,2,3], [1,2,3])
True
>>> compare([1,2,3,3], [1,2,2,3])
False
>>>
You want to see if they contain the same elements, but don’t care about the order.
You can use a set:
>>> set(['one', 'two', 'three']) == set(['two', 'one', 'three'])
True
But the set object itself will only contain one instance of each unique value, and will not preserve order.
>>> set(['one', 'one', 'one']) == set(['one'])
True
So, if tracking duplicates/length is important, you probably want to also check the length:
def are_eq(a, b):
return set(a) == set(b) and len(a) == len(b)
If elements are always nearly sorted as in your example then builtin .sort()
(timsort) should be fast:
>>> a = [1,1,2]
>>> b = [1,2,2]
>>> a.sort()
>>> b.sort()
>>> a == b
False
If you don’t want to sort inplace you could use sorted()
.
In practice it might always be faster then collections.Counter()
(despite asymptotically O(n)
time being better then O(n*log(n))
for .sort()
). Measure it; If it is important.
if you do not want to use the collections library, you can always do something like this:
given that a
and b
are your lists, the following returns the number of matching elements (it considers the order).
sum([1 for i,j in zip(a,b) if i==j])
Therefore,
len(a)==len(b) and len(a)==sum([1 for i,j in zip(a,b) if i==j])
will be True
if both lists are the same, contain the same elements and in the same order. False
otherwise.
So, you can define the compare function like the first response above,but without the collections library.
compare = lambda a,b: len(a)==len(b) and len(a)==sum([1 for i,j in zip(a,b) if i==j])
and
>>> compare([1,2,3], [1,2,3,3])
False
>>> compare([1,2,3], [1,2,3])
True
>>> compare([1,2,3], [1,2,4])
False
sorted(x) == sorted(y)
Copying from here: Check if two unordered lists are equal
I think this is the best answer for this question because
- It is better than using counter as pointed in this answer
- x.sort() sorts x, which is a side effect. sorted(x) returns a new list.
One liner answer to the above question is :-
let the two lists be list1 and list2,
and your requirement is to ensure whether two lists have the same elements, then as per me, following will be the best approach :-
if ((len(list1) == len(list2)) and
(all(i in list2 for i in list1))):
print 'True'
else:
print 'False'
The above piece of code will work per your need i.e. whether all the elements of list1 are in list2 and vice-verse.
But if you want to just check whether all elements of list1 are present in list2 or not, then you need to use the below code piece only :-
if all(i in list2 for i in list1):
print 'True'
else:
print 'False'
The difference is, the later will print True, if list2 contains some extra elements along with all the elements of list1. In simple words, it will ensure that all the elements of list1 should be present in list2, regardless of whether list2 has some extra elements or not.
Assuming you already know lists are of equal size, the following will guarantee True if and only if two vectors are exactly the same (including order)
functools.reduce(lambda b1,b2: b1 and b2, map(lambda e1,e2: e1==e2, listA, ListB), True)
Example:
>>> from functools import reduce
>>> def compvecs(a,b):
... return reduce(lambda b1,b2: b1 and b2, map(lambda e1,e2: e1==e2, a, b), True)
...
>>> compvecs(a=[1,2,3,4], b=[1,2,4,3])
False
>>> compvecs(a=[1,2,3,4], b=[1,2,3,4])
True
>>> compvecs(a=[1,2,3,4], b=[1,2,4,3])
False
>>> compare_vectors(a=[1,2,3,4], b=[1,2,2,4])
False
>>>
What about getting the string representation of the lists and comparing them ?
>>> l1 = ['one', 'two', 'three']
>>> l2 = ['one', 'two', 'three']
>>> l3 = ['one', 'three', 'two']
>>> print str(l1) == str(l2)
True
>>> print str(l1) == str(l3)
False
I’m looking for an easy (and quick) way to determine if two unordered lists contain the same elements:
For example:
['one', 'two', 'three'] == ['one', 'two', 'three'] : true
['one', 'two', 'three'] == ['one', 'three', 'two'] : true
['one', 'two', 'three'] == ['one', 'two', 'three', 'three'] : false
['one', 'two', 'three'] == ['one', 'two', 'three', 'four'] : false
['one', 'two', 'three'] == ['one', 'two', 'four'] : false
['one', 'two', 'three'] == ['one'] : false
I’m hoping to do this without using a map.
Python has a built-in datatype for an unordered collection of (hashable) things, called a set
. If you convert both lists to sets, the comparison will be unordered.
set(x) == set(y)
EDIT: @mdwhatcott points out that you want to check for duplicates. set
ignores these, so you need a similar data structure that also keeps track of the number of items in each list. This is called a multiset; the best approximation in the standard library is a collections.Counter
:
>>> import collections
>>> compare = lambda x, y: collections.Counter(x) == collections.Counter(y)
>>>
>>> compare([1,2,3], [1,2,3,3])
False
>>> compare([1,2,3], [1,2,3])
True
>>> compare([1,2,3,3], [1,2,2,3])
False
>>>
You want to see if they contain the same elements, but don’t care about the order.
You can use a set:
>>> set(['one', 'two', 'three']) == set(['two', 'one', 'three'])
True
But the set object itself will only contain one instance of each unique value, and will not preserve order.
>>> set(['one', 'one', 'one']) == set(['one'])
True
So, if tracking duplicates/length is important, you probably want to also check the length:
def are_eq(a, b):
return set(a) == set(b) and len(a) == len(b)
If elements are always nearly sorted as in your example then builtin .sort()
(timsort) should be fast:
>>> a = [1,1,2]
>>> b = [1,2,2]
>>> a.sort()
>>> b.sort()
>>> a == b
False
If you don’t want to sort inplace you could use sorted()
.
In practice it might always be faster then collections.Counter()
(despite asymptotically O(n)
time being better then O(n*log(n))
for .sort()
). Measure it; If it is important.
if you do not want to use the collections library, you can always do something like this:
given that a
and b
are your lists, the following returns the number of matching elements (it considers the order).
sum([1 for i,j in zip(a,b) if i==j])
Therefore,
len(a)==len(b) and len(a)==sum([1 for i,j in zip(a,b) if i==j])
will be True
if both lists are the same, contain the same elements and in the same order. False
otherwise.
So, you can define the compare function like the first response above,but without the collections library.
compare = lambda a,b: len(a)==len(b) and len(a)==sum([1 for i,j in zip(a,b) if i==j])
and
>>> compare([1,2,3], [1,2,3,3])
False
>>> compare([1,2,3], [1,2,3])
True
>>> compare([1,2,3], [1,2,4])
False
sorted(x) == sorted(y)
Copying from here: Check if two unordered lists are equal
I think this is the best answer for this question because
- It is better than using counter as pointed in this answer
- x.sort() sorts x, which is a side effect. sorted(x) returns a new list.
One liner answer to the above question is :-
let the two lists be list1 and list2,
and your requirement is to ensure whether two lists have the same elements, then as per me, following will be the best approach :-
if ((len(list1) == len(list2)) and
(all(i in list2 for i in list1))):
print 'True'
else:
print 'False'
The above piece of code will work per your need i.e. whether all the elements of list1 are in list2 and vice-verse.
But if you want to just check whether all elements of list1 are present in list2 or not, then you need to use the below code piece only :-
if all(i in list2 for i in list1):
print 'True'
else:
print 'False'
The difference is, the later will print True, if list2 contains some extra elements along with all the elements of list1. In simple words, it will ensure that all the elements of list1 should be present in list2, regardless of whether list2 has some extra elements or not.
Assuming you already know lists are of equal size, the following will guarantee True if and only if two vectors are exactly the same (including order)
functools.reduce(lambda b1,b2: b1 and b2, map(lambda e1,e2: e1==e2, listA, ListB), True)
Example:
>>> from functools import reduce
>>> def compvecs(a,b):
... return reduce(lambda b1,b2: b1 and b2, map(lambda e1,e2: e1==e2, a, b), True)
...
>>> compvecs(a=[1,2,3,4], b=[1,2,4,3])
False
>>> compvecs(a=[1,2,3,4], b=[1,2,3,4])
True
>>> compvecs(a=[1,2,3,4], b=[1,2,4,3])
False
>>> compare_vectors(a=[1,2,3,4], b=[1,2,2,4])
False
>>>
What about getting the string representation of the lists and comparing them ?
>>> l1 = ['one', 'two', 'three']
>>> l2 = ['one', 'two', 'three']
>>> l3 = ['one', 'three', 'two']
>>> print str(l1) == str(l2)
True
>>> print str(l1) == str(l3)
False