gpt4 book ai didi

python - 将分层关键字与文档匹配

转载 作者:太空宇宙 更新时间:2023-11-04 01:35:07 25 4
gpt4 key购买 nike

我有一个分层关键字树,表示为元组列表,其中第一个参数是“路径”,第二个是相应的关键字:

keys = [('0','key1'),('0,1','key2'),('0,1,12','key3'),('0,2','key4'),('0,2,30','key5')]

列出连接'路径'和对应文档(一个文档可以有多个'路径':

docs = [('0,1,12','doc1'),('0,2,30','doc1'),('0,1','doc2')]

我想将每个文档与关键字匹配并产生如下结果:

docdict={doc1:[('key1','key2','key3'),('key1','key4','key5')],doc2:[('key1','key2')]}

我的问题是如何最有效地获取所有(父)关键字?提前致谢!

最佳答案

一个更具可读性的答案,如果你有很多这样的答案,它可能会更好地扩展。

docs = [('0,1,12','doc1'),('0,2,30','doc1'),('0,1','doc2')]
keys = [('0','key1'),('0,1','key2'),('0,1,12','key3'),('0,2','key4'),('0,2,30','key5')]

keydict = dict(keys)
resultDict = {}

for doc in docs:
(path, docname) = doc
pathList = path.split(',')
keyPath = []
for i in range(0, len(pathList)):
aPath = ','.join(pathList[:i+1])
keyPath.append(keydict[aPath])

if docname not in resultDict :
resultDict[docname] = []
resultDict[docname].append(tuple(keyPath))

print resultDict

关于python - 将分层关键字与文档匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10616515/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com