我有一个字典字典。对于字典中的每个键,都有一个列表,其中有两个项目。一个是另一个字典,另一个是整数。Python:从字典中删除顶部'n'键
dict = {
'hello' : [
{
'blah' : 1,
'dodo' : 2
},
3
],
'world' : [
{
'foo' : 7,
'bar' : 1
},
8
]
}
我想对列表中的第二个项目整数字典词典进行排序。然后从字典中删除第一个'n'键。有什么办法可以做到吗?排序后的函数仅适用于列表。
这里是我想做到这一点的功能。
def create_inverted_index(inverted_index, corpus_tokens, corpus_files):
for file_tokens in corpus_tokens:
file_id = corpus_files[file_tokens[0]]
for token in file_tokens[1]:
if token in inverted_index.keys():
inverted_index[token][1] += 1
if file_id in inverted_index[token][0].keys():
inverted_index[token][0][file_id] += 1
else:
inverted_index[token][0][file_id] = 1
else:
inverted_index[token] = [{file_id : 1}, 1]
这是你的实际字典吗?当我尝试运行它时,出现'TypeError:unhashable type:'list''。 – Kevin 2014-09-12 13:02:45
@Kevin似乎是缺少主字典的两个键。 – Nras 2014-09-12 13:04:56
您可以发布工作代码吗? – khelwood 2014-09-12 13:05:47