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.

有帮助吗?

解决方案

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])

其他提示

{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}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top