-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