How can I print just the differences between two tuples?
>>>a=(1,2,3,4)
>>>b=(2,3,5,6)
If the tuples are as above then the expected output is (1,4,5,6). The compare functions can only compare but could not find the differences.
The tuples are generated by MySQLdb python library. The queries being executed produces a fair amount of data.
In [13]: c.execute(query)
Out[13]: 86844L
In [14]: qop1 = c.fetchall()
In [19]: len(qop1)
Out[19]: 86844
So if I compare every tuple with custom code it will take a lot of time. Can I achieve this an efficient way like an inbuilt python library?
You want the Symmetric Difference -- elements in either set but not in the intersection. This can be done by:
set(a) ^ set(b)
or:
set(a).symmetric_difference(set(b))
From your example one can deduce that you are asking about symmetric difference of sets and in python you can simply do
a=(1,2,3,4)
b=(2,3,5,6)
print tuple(set(a) ^ set(b))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With