'set difference and discard order [duplicate]
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?
Solution 1:[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()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | John Coleman |
