如何将此元组列表更改为以下格式?
How do I change this list of tuples to the following format?
我正在尝试更改以下元组:
[((' "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}
我尝试将其更改为字典(这是不可能的,因为它不知道如何分配键和值)和列表,但似乎没有任何点击。
我目前正在使用 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()
如何更改我的输出写入以获得我想要的格式?
从你编写代码的文件来看,你的元组格式似乎是一致的,所以如果这是真的,那是微不足道的:
x = ((' "business_id": "zPBccKsIHYtLUGFNYIi8Uw"', ' "business_id": "znDUBjt-m2qmXi_p3m3rDA"'), 0.09523809523809523)
yy = {}
yy['b1'] = x[0][0].split(':')[1].strip('" ')
yy['b2'] = x[0][1].split(':')[1].strip('" ')
yy['sim'] = x[1]
您知道元组中所有成员的位置,因此获取您想要的值,删除多余的引号和空格,然后将其全部打包到字典中。
如果您的元组保持这种格式,那么您需要进行一些字符串解析,以便从元组的第一个元素中获取 ID。 split
是这种情况下最好的方法。对于您当前的格式,此代码有效,但您绝对可以将其推广到其他情况。
def fix_tuple(tup):
ids = tup[0]
id1 = ids[0].split('"')[-2]
id2 = ids[1].split('"')[-2]
return {'b1':id1,'b2':id2,'sim':tup[1]}
进行中:
tup = ((' "business_id": "zPBccKsIHYtLUGFNYIi8Uw"', ' "business_id": "znDUBjt-m2qmXi_p3m3rDA"'), 0.09523809523809523)
print(fix_tuple(tup))
# Output: {'b1': 'zPBccKsIHYtLUGFNYIi8Uw', 'b2': 'znDUBjt-m2qmXi_p3m3rDA', 'sim': 0.09523809523809523}
import re
KV_RE = re.compile(r'"(?P<key>[^"]+)":\s*"(?P<value>[^"]+)"')
getvalue = lambda kv: match.group("value") if (match := KV_RE.search(kv)) else None
def dict_from_pair(pair):
((kv1, kv2), sim) = pair
return dict(b1=getvalue(kv1), b2=getvalue(kv2), sim=sim)
pair = ((' "business_id": "zPBccKsIHYtLUGFNYIi8Uw"', ' "business_id": "znDUBjt-m2qmXi_p3m3rDA"'), 0.09523809523809523)
print(dict_from_pair(pair))
# {'b1': 'zPBccKsIHYtLUGFNYIi8Uw', 'b2': 'znDUBjt-m2qmXi_p3m3rDA', 'sim': 0.09523809523809523}
我正在尝试更改以下元组:
[((' "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}
我尝试将其更改为字典(这是不可能的,因为它不知道如何分配键和值)和列表,但似乎没有任何点击。
我目前正在使用 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()
如何更改我的输出写入以获得我想要的格式?
从你编写代码的文件来看,你的元组格式似乎是一致的,所以如果这是真的,那是微不足道的:
x = ((' "business_id": "zPBccKsIHYtLUGFNYIi8Uw"', ' "business_id": "znDUBjt-m2qmXi_p3m3rDA"'), 0.09523809523809523)
yy = {}
yy['b1'] = x[0][0].split(':')[1].strip('" ')
yy['b2'] = x[0][1].split(':')[1].strip('" ')
yy['sim'] = x[1]
您知道元组中所有成员的位置,因此获取您想要的值,删除多余的引号和空格,然后将其全部打包到字典中。
如果您的元组保持这种格式,那么您需要进行一些字符串解析,以便从元组的第一个元素中获取 ID。 split
是这种情况下最好的方法。对于您当前的格式,此代码有效,但您绝对可以将其推广到其他情况。
def fix_tuple(tup):
ids = tup[0]
id1 = ids[0].split('"')[-2]
id2 = ids[1].split('"')[-2]
return {'b1':id1,'b2':id2,'sim':tup[1]}
进行中:
tup = ((' "business_id": "zPBccKsIHYtLUGFNYIi8Uw"', ' "business_id": "znDUBjt-m2qmXi_p3m3rDA"'), 0.09523809523809523)
print(fix_tuple(tup))
# Output: {'b1': 'zPBccKsIHYtLUGFNYIi8Uw', 'b2': 'znDUBjt-m2qmXi_p3m3rDA', 'sim': 0.09523809523809523}
import re
KV_RE = re.compile(r'"(?P<key>[^"]+)":\s*"(?P<value>[^"]+)"')
getvalue = lambda kv: match.group("value") if (match := KV_RE.search(kv)) else None
def dict_from_pair(pair):
((kv1, kv2), sim) = pair
return dict(b1=getvalue(kv1), b2=getvalue(kv2), sim=sim)
pair = ((' "business_id": "zPBccKsIHYtLUGFNYIi8Uw"', ' "business_id": "znDUBjt-m2qmXi_p3m3rDA"'), 0.09523809523809523)
print(dict_from_pair(pair))
# {'b1': 'zPBccKsIHYtLUGFNYIi8Uw', 'b2': 'znDUBjt-m2qmXi_p3m3rDA', 'sim': 0.09523809523809523}