If I have for example two vectors a
and b
,
a = [1, 3, 6]
b = [3, 1, 6]
since the content of the vectors is the same, is it possible in some way to compare them and get true as a result?
If I have for example two vectors a
and b
,
a = [1, 3, 6]
b = [3, 1, 6]
since the content of the vectors is the same, is it possible in some way to compare them and get true as a result?
You can use collections.Counter
:
from collections import Counter
Counter(a) == Counter(b)
You can use sorted
and then compare. As pointed out by blhsing, this is O(n log n) operation whereas the solution with Counter
is O(n). Since n=3
in your case, the difference will be negligible but the difference will become apparent for large n
. You might be interested in knowing this.
a = [1, 3, 6]
b = [3, 1, 6]
sorted(a) == sorted(b)
# True
Here you will find extensive discussion on this topic.