0

I have the following sets:

>>> s2 = set(((1,3),(2,3,6,4)))
>>> s1 = set(((1,3),(2,3,4,6)))
>>> s1.symmetric_difference(s2)
{(2, 3, 4, 6), (2, 3, 6, 4)}

I want to compare two above sets but set discards order of them, It means It return null set such as:

>>> s1.symmetric_difference(s2)
set()

UPDATE:

Indeed when sequence of two sets are difference, I want to sets supposes they are one set. How can I define it with sets in python?

PersianGulf
  • 2,845
  • 6
  • 47
  • 67
  • @JohnColeman oh no, empty means When I have two set and their value are same together but seqence of them is different. When sequence will be different, I want to set supposes they are same one set. – PersianGulf Feb 27 '22 at 13:11

2 Answers2

2

Store your sets not as tuples but as sets. Since you need them hashable, use frozensets. Like frozenset((1,3)) instead of (1,3).

Btw you can also get the symmetric difference with s1 ^ s2.

Pychopath
  • 1,560
  • 1
  • 8
1

One way to make order irrelevant is to make it the same. You could sort the tuples prior to putting them in a set:

>>> s2 = set(tuple(sorted(s)) for s in (((1,3),(2,3,6,4))))
>>> s1 = set(tuple(sorted(s)) for s in (((1,3),(2,3,4,6))))
>>> s1.symmetric_difference(s2)
set()
John Coleman
  • 51,337
  • 7
  • 54
  • 119