사전 사전이 있습니다. dict의 각 키에는 두 개의 항목이 들어있는 목록이 있습니다. 하나는 다른 사전이고 다른 하나는 정수입니다.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
@Kevin은 주요 dict에 대한 두 개의 키가 누락 된 것처럼 보입니다. – Nras
작업 코드를 게시 할 수 있습니까? – khelwood