ここから、各タプルの2番目の項目が重複している場合、タプルのリストから要素を削除するにはどうすればよいですか?、タプルの1つのリストからタプルの2番目の要素の重複を削除できます。
タプルのリストが2つあるとしましょう。
alist = [(0.7897897,'this is a foo bar sentence'),
(0.653234, 'this is a foo bar sentence'),
(0.353234, 'this is a foo bar sentence'),
(0.325345, 'this is not really a foo bar'),
(0.323234, 'this is a foo bar sentence'),]
blist = [(0.64637,'this is a foo bar sentence'),
(0.534234, 'i am going to foo bar this sentence'),
(0.453234, 'this is a foo bar sentence'),
(0.323445, 'this is not really a foo bar')]
そして、2番目の要素が同じである場合(score_from_alist * score_from_blist)、スコアを組み合わせて、目的の出力を達成する必要があります。
clist = [(0.51,'this is a foo bar sentence'), # 0.51 = 0.789 * 0.646
(0.201, 'this is not really a foo bar')] # 0.201 = 0.325 * 0.323
現在、これを行うことでclistを達成していますが、alistとblistに約5500以上のタプルがあり、2番目の要素にそれぞれ約20〜40語がある場合、5秒以上かかります。次の機能を高速化する方法はありますか?
def overlapMatches(alist, blist):
start_time = time.time()
clist = []
overlap = set()
for d in alist:
for dn in blist:
if d[1] == dn[1]:
score = d[0]*dn[0]
overlap.add((score,d[1]))
for s in sorted(overlap, reverse=True)[:20]:
clist.append((s[0],s[1]))
print "overlapping matches takes", time.time() - start_time
return clist