-1
有两个级别的字典中的Python:如何提取多层次的字典键/值蟒蛇
比如这里:index[term][id] = n
如何获得term
和n
时id = 3
?
或者,如果它的形式返回类似result[id] = [term, n]
有两个级别的字典中的Python:如何提取多层次的字典键/值蟒蛇
比如这里:index[term][id] = n
如何获得term
和n
时id = 3
?
或者,如果它的形式返回类似result[id] = [term, n]
遍历嵌套的字典,并创造新的字典中的值所需格式映射这将是完美的。您可以创建自定义的功能,如:
def get_tuple_from_value(my_dict):
new_dict = {}
for term, nested_dict in my_dict.items():
for id, n in nested_dict.items():
new_dict[id] = [term, n]
return new_dict
OR,简单字典理解看起来像:
{i: [t, n] for t, nd in d.items() for i, n in nd.items()}
其中d
拿着你的字典。
是独特的ID吗?你能提供实际字典的样子吗? – depperm
是的,这个ID是唯一的,索引实际上存储了文档集合中的词(词)。 index ['和'] [456] = 7意味着在documnet456中,'和'出现7次。 – runchu
你可以将你的字典翻转为'index [documentId] [word] = count' – depperm