-1

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?

Sheldore
  • 37,862
  • 7
  • 57
  • 71
  • Check [link](https://stackoverflow.com/questions/9623114/check-if-two-unordered-lists-are-equal) – Rohit-Pandey Nov 01 '18 at 17:01
  • I can not use 'sort' because in reality I have to compare only one piece of vectors. my mistake because I have not explained my problem well, I wanted to avoid not to complicate the question – Dumitru Galbura Nov 01 '18 at 17:02

3 Answers3

2

You can use collections.Counter:

from collections import Counter
Counter(a) == Counter(b)
blhsing
  • 91,368
  • 6
  • 71
  • 106
1

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.

Sheldore
  • 37,862
  • 7
  • 57
  • 71
0

Try This:

 set(a) == set(b)

Because set automatically sort.

Rohit-Pandey
  • 2,039
  • 17
  • 24