Determine if 2 lists have the same elements regardless of order?
The conclusion from your example:
x = ['a', 'b'] y = ['b', 'a']
that the elements of the lists will not be repeated (they are unique), as well as hashable (which lines and other specific python immutable objects), the most direct and computationally efficient Python answer to the built-in sets (which are semantically similar mathematical sets about which you are possibly , learned at school).
set(x) == set(y)
In case the items are hashed, but not unique, collections.Counter also works semantically as a multiset, but it is much slower:
from collections import Counter Counter(x) == Counter(y)
Prefer to use sorted :
sorted(x) == sorted(y)
if items are ordered. This will not take into account unique or faulty circumstances, but it can be much slower than using sets.
Empirical experiment
In an empirical experiment, it is concluded that set should be preferred, then sorted . Use only Counter if you need other things, such as counting or further use as a multiset.
First setup:
import timeit import random from collections import Counter data = [str(random.randint(0, 100000)) for i in xrange(100)] data2 = data[:] # copy the list into a new one def sets_equal(): return set(data) == set(data2) def counters_equal(): return Counter(data) == Counter(data2) def sorted_lists_equal(): return sorted(data) == sorted(data2)
And testing:
>>> min(timeit.repeat(sets_equal)) 13.976069927215576 >>> min(timeit.repeat(counters_equal)) 73.17287588119507 >>> min(timeit.repeat(sorted_lists_equal)) 36.177085876464844
So, we see that comparing sets is the fastest solution, and comparing sorted lists is the second fastest.
Aaron Hall 02 Oct '14 at 15:26 2014-10-02 15:26
source share