0

What is the best practice for finding out from which set the results of symmetric_difference are from?

intersect = s1.symmetric_difference(s2)

The result should look like

{'34':'s1', '66':'s2'} 

Where '34','66' are the unique items.

LarsVegas
  • 6,522
  • 10
  • 43
  • 67

2 Answers2

3

To do this most cleanly, the following should work:

intersect = s1.symmetric_difference(s2)
result = dict([(i, ("s1" if i in s1 else "s2")) for i in intersect])
qaphla
  • 4,707
  • 3
  • 20
  • 31
  • `dict([(x,y) for ...])` can be better written as `dict((x,y) for ...)`. The additional brackets create an unnecessary list that is then immediately converted to a dictionary, so not using them will both look cleaner and run faster. – Matt Bryant Aug 09 '13 at 05:56
1
{x : 's1' for x in intersect if x in s1} + {x : 's2' for x in intersect if x in s2}

or

{x : ('s1' if x in s1 else 's2') for x in intersect}
YXD
  • 31,741
  • 15
  • 75
  • 115