我正在尝试更改以下元组:
[((' "business_id": "zPBccKsIHYtLUGFNYIi8Uw"', ' "business_id": "znDUBjt-m2qmXi_p3m3rDA"'), 0.09523809523809523),((' "business_id": "zauhMY78k36XPfxD3GURkQ"', ' "business_id": "zp-K5s3pGTWuuaVBWo6WZA"'), 0.07407407407407407)]
到这种格式:
{'b1': 'zPBccKsIHYtLUGFNYIi8Uw', 'b2': "znDUBjt-m2qmXi_p3m3rDA', 'sim': 0.09523809523809523}
我尝试通过尝试将其更改为 dict (这是不可能的,因为它不知道如何分配键和值)和列表来解决此问题,但似乎没有任何点击。
我目前正在使用 Python 将元组写入输出文件:
fout = open(outfilePath, mode = 'w')
fwriter = csv.writer(fout, delimiter = ',', quoting = csv.QUOTE_MINIMAL)
for pair in similarPairs:
fwriter.writerow([ str(pair[0][0]), str(pair[0][1]), pair[1]])
fout.close()
我使用阈值 >=0.055 的 Jaccard 相似度找到相似对:
## Computing the Jaccard Similarity for the candidate pairs.
similarPairs = candidatePairs.map(lambda currPair : computeJC(currPair, ratedBusinessUsers)).filter(lambda f : f[1] >= 0.055).collect()
如何更改输出写入以获得我想要的格式?
临摹微笑
阿晨1998
慕沐林林
相关分类