def query_RR(postings, qtext):
words = tokenize(qtext)
allpostings = [postings[w] for w in words]
for a in allpostings:
print a.keys()
这是查询的结果
[0, 2, 3, 4, 6]
[1, 4, 5]
[0, 2, 4]
[4, 5]
该查询采用用户输入术语(qtext
),标记化并为每个令牌生成发布列表。
发布列表是嵌套字典的列表。例如
[{0 : 0.68426, 1: 0.26423}, {2: 0.6842332, 0: 0.9823}]
对于公共密钥,您将如何访问相应的值,对于我的示例,常见值是0
so,您将如何访问值{0 : 0.68426, 0: 0.9823}
?
这是一种方式:
lst = [{0: 0.68426, 1: 0.26423}, {2: 0.6842332, 0: 0.9823}]
[d[0] for d in lst] # [0.68426, 0.9823]
或者如果你需要一本字典:
{0: [d[0] for d in lst]} # {0: [0.68426, 0.9823]}
如果您需要计算常用字典键:
{i: [d[i] for d in lst] for i in set.intersection(*map(set, lst))}
# {0: [0.68426, 0.9823]}
请注意,字典键必须是唯一的,因此无法输出所需的输出。
lst = [{0 : 0.68426, 1: 0.26423}, {2: 0.6842332, 0: 0.9823}]
res = {}
for x in lst:
for key, value in x.items():
if key in res:
res[key].append(value)
else:
res[key] = [value]
这会返回一个{0: [0.68426, 0.9823]}
形式的字典,因为这个{0 : 0.68426, 0: 0.9823}
是不可能的。