SQLite compare query Python
I've been trying to figure out the best way to write a query to compare the rows in two tables. My goal is to see if the two tuples in result Set A are in the larger result set B. I only want to see the tuples that are different in the query results.
'''SELECT table1.field_b, table1.field_c, table1.field_d
'''FROM table1
'''ORDER BY field_b
results_a = [(101010101, 111111111, 999999999), (121212121, 222222222, 999999999)]
'''SELECT table2.field_a, table2.fieldb, table3.field3
'''FROM table2
'''OR开发者_Python百科DER BY field_a
results_b =[(101010101, 111111111, 999999999), (121212121, 333333333, 999999999), (303030303, 444444444, 999999999)]
So what I want to do is take results_a and make sure that they have an exact match somewhere in results_b. So since the second record in the second tuple is different than what is in results_a, I would like to return the second tuple in results_a.
Ultimately I would like to return a set that also has the second tuple that did not match in the other set so I could reference both in my program. Ideally since the second tuples primary key (field_b in table1) didn't match the corresponding primary key (field_a) in table2 then I would want to display results_c ={(121212121, 222222222, 999999999):(121212121, 222222222, 999999999)}. This is complicated by the facts that the results in both tables will not be in the same order so I can't write code that says (compare tuple2 in results_a to tuple2 in results_b). It is more like (compare tuple2 in results_a and see if it matches any record in results_b. If the primary keys match and none of the tuples in results b completely match or no partial match is found return the records that don't match.)
I apologize that this is so wordy. I couldn't think of a better way to explain it. Any help would be much appreciated.
Thanks!
UPDATED EFFORT ON PARTIAL MATCHES
a = [(1, 2, 3),(4,5,7)]
b = [(1, 2, 3),(4,5,6)]
pmatch = dict([])
def partial_match(x,y):
return sum(ea == eb for (ea,eb) in zip(x,y))>=2
for el_a in a:
pmatch[el_a] = [el_b for el_b in b if partial_match(el_a,el_b)]
print(pmatch)
OUTPUT = {(4, 5, 7): [(4, 5, 6)], (1, 2, 3): [(1, 2, 3)]}. I would have expected it to be just {(4,5,7):(4,5,6)} because those are the only sets that are different. Any ideas?
Take results_a and make sure that they have an exact match somewhere in results_b:
for el in results_a:
if el in results_b:
...
Get partial matches:
pmatch = dict([])
def partial_match(a,b):
# for instance ...
return sum(ea == eb for (ea,eb) in zip(a,b)) >= 2
for el_a in results_a:
pmatch[el_a] = [el_b for el_b in results_b if partial_macth(el_a,el_b)]
Return the records that don't match:
no_match = [el for el in results_a if el not in results_b]
-- EDIT / Another possible partial_match
def partial_match(x,y):
nb_matches = sum(ea == eb for (ea,eb) in zip(x,y))
return 0.6 < float(nb_matches) / len(x) < 1
精彩评论