[Tutor] Comparing nested tuples of tuples
Wed, 27 Oct 1999 08:40:12 -0700
The situation is two tuples of tuples (returned from a db) that I want to
compare for differences and return the differences, including a mathematical
assume I have two tuples of tuples:
i = (('chevy',30),('spam',500),('ford',60),('chrysler',90),('honda',45))
v = (('chevy',30),('ford',90),('chrysler',90),('honda',45),('eggs',30))
If I wanted to compare the two, I could simply do some filters:
tuples the same: filter(lambda x: x in i,v)
which would return me the nested chevy, honda and chrysler tuples
tuples unique to i: filter(lambda x: x not in v,i)
which would return me the nested ford and spam tuples
tuples unique to v: filter(lambda x: x not in i,v)
which would return me the nested ford and eggs tuples.
But I want to go deeper in two ways:
I want to see those unique *first item* nested tuples within each tuple.
i.e. I want to write a function that will return just spam (not include
ford) as unique to i and another function that returns eggs (also not ford)
unique to v when compared to i.
then an additional function would return the mathematical subtraction of
second item nested tuples with identical first item nested tuples in both
tuples. in this case only return 'ford' and 30 (+ or - depending upon
direction of comparison).
one caveat, is that the two tuples of tuples would *not* be of the same
length or in any reasonable sort order. The first item in a nested tuple
would not necessarily show up in the other. However, the first item within
each nested tuple would be unique within each tuple. Also, each tuple could
have many thousands of nested tuples.
would my best bet be to use nested if statements and a step through each
nested tuple and compare it to every nested tuple in the other tuple, albeit
analyzing possible thousands of nested tuples over and over in the opposite
tuple, or I was thinking of converting the tuples to dictionaries and then
using keys? Though this seems like alot of overhead too.
how would you approach this?