table a (t_a):
id name last first email state country
0 sklass klass steve sklass@foo.com in uk
1 jabid abid john abid@foo.com ny us
2 jcolle colle john jcolle@foo.com wi us
table b (t_b):
id sn given nick email l c
0 steven klass steve sklass@foo.com in uk
1 john abid - abid_j@foo.com ny us
2 johnny colle john jcolle@foo.com wi us
3 john abid - abid@foo.com ny us
上面列出的是一个(缩写)列和行mySQL表。看看这两个表,可以很清楚地看到,通过严格地查看值(没有查看id)并比较匹配的值数量,您将得到这些值匹配。在
^{pr2}$我最终要做的是在Django做这个-我不确定这是否重要。在过去,我使用纯python来完成这项工作,在python中销毁旧数据,只创建三个新表。我想离开我的实现(如下所列),因为我看到的问题是时间改变了事情,人来了又走。过去我只是重新生成数据,但现在我想跟踪人们何时离开,而不是简单地替换(删除)数据。我相信通过SQL更新可以更优雅地保存历史。在
我想知道如何直接从mySQL(一个SQL函数或一个新表的构造)中得到这个合并的答案,mySQL按以下方式合并数据。我想用纯SQL来实现这一点(我相信我可以用Django实现)。因此,我正在寻找满足以下条件的解决方案:
现在我的背景是python,对我来说,最简单的方法就是对两个表中较短的一个表执行for循环,然后在另一个表上执行for循环,查看匹配的数量。在代码中是这样的。在
t_a = [ ["sklass", "klass", "steve", "sklass@foo.com", "in", "uk", ],
["jabid", "abid", "john", "abid@foo.com", "ny", "us", ],
["jcolle", "colle", "john", "jcolle@foo.com", "wi", "us", ], ]
t_b = [ ["steven", "klass", "steve", "sklass@foo.com", "in", "uk",],
["john", "abid", "abid_j@foo.com", "ny", "us",],
["johnny", "colle", "john", "jcolle@foo.com", "wi", "us",],
["john", "abid", "abid@foo.com", "ny", "us",], ]
min_match = 3
for person_a in t_a:
match = 0
match_pct = 0.0
match_a_index = t_a.index(person_a)
for person_b in t_b:
new_match_count = len(list(set(person_a) & set(person_b)))
if new_match_count > match:
match = new_match_count
match_b_index = t_b.index(person_b)
match_pct = "%.2f" % (float(new_match_count) / \
float(len(set(person_a + person_b))) * 100)
if match >= min_match:
print match_a_index, match_b_index #, match_pct, match
这些评论提出了一个问题:你为什么不直接在电子邮件地址上加入呢。我不一定知道列中的值是否匹配。我确信t_a中给定行的值将与t_b中的某一行的值匹配。我希望t_a中给定行的最高(最可能)匹配到t_b,并且仅当匹配数大于min_match时。在
您可以在MySQL中直接通过通过存储过程执行的游标来实现这一点。在
相关问题 更多 >
编程相关推荐