2 回答

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超2個(gè)贊
這里有一個(gè)解決方案:
d = {}
for key,list_dict in dict1.items():
d[key] = sorted([k for k in list_dict if list(k.items())[0][1] >0], key=lambda x: list(x.items())[0][1], reverse=True)
d
輸出:
{'He': [{'doc_2': 1}],
'His': [{'doc_1': 1}, {'doc_3': 1}],
'I': [{'doc_1': 1}],
'Manager': [{'doc_2': 1}],
'Project': [{'doc_2': 1}],
'T': [{'doc_1': 1}],
'area': [{'doc_1': 1}],
'areas': [{'doc_2': 1}],
'assessments': [{'doc_2': 1}],
'associated': [{'doc_1': 1}],
'big': [{'doc_3': 1}],
'billing': [{'doc_2': 1}],
'book': [{'doc_3': 1}],
'data': [{'doc_3': 3}],
'different': [{'doc_1': 1}, {'doc_2': 1}],
'domain': [{'doc_1': 1}, {'doc_2': 1}],
'endoscopy': [{'doc_2': 1}],
'experience': [{'doc_1': 1}],
'favorite': [{'doc_3': 1}],
'financial': [{'doc_1': 1}, {'doc_2': 1}],
'interest': [{'doc_3': 1}],
'major': [{'doc_2': 1}],
'organizations': [{'doc_1': 1}],
'patient': [{'doc_2': 2}],
'radiation': [{'doc_2': 1}],
'science': [{'doc_3': 2}],
'work': [{'doc_1': 1}],
'working': [{'doc_1': 1}, {'doc_2': 1}],
'years': [{'doc_1': 1}]}

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超13個(gè)贊
這應(yīng)該可以解決您的問題:-
res = {}
for key, value in dict1.items():
res[key] = [i for i in dict1[key] if 0 not in (i.values())]
res[key] = sorted(res[key], key=lambda x : x[list(x.keys())[0]], reverse=True)
print(res)
輸出:-
{'years': [{'doc_1': 1}], 'working': [{'doc_1': 1}, {'doc_2': 1}],
'experience': [{'doc_1': 1}], 'I': [{'doc_1': 1}], 'T': [{'doc_1': 1}],
'associated': [{'doc_1': 1}], 'different': [{'doc_1': 1}, {'doc_2': 1}],
'organizations': [{'doc_1': 1}], 'His': [{'doc_1': 1}, {'doc_3': 1}],
'work': [{'doc_1': 1}], 'domain': [{'doc_1': 1}, {'doc_2': 1}],
'area': [{'doc_1': 1}], 'financial': [{'doc_1': 1}, {'doc_2': 1}],
'Project': [{'doc_2': 1}], 'Manager': [{'doc_2': 1}], 'He': [{'doc_2': 1}],
'areas': [{'doc_2': 1}], 'major': [{'doc_2': 1}], 'patient': [{'doc_2': 2}],
'billing': [{'doc_2': 1}], 'assessments': [{'doc_2': 1}], 'radiation': [{'doc_2': 1}],
'endoscopy': [{'doc_2': 1}], 'interest': [{'doc_3': 1}], 'data': [{'doc_3': 3}],
'science': [{'doc_3': 2}], 'favorite': [{'doc_3': 1}], 'book': [{'doc_3': 1}],
'big': [{'doc_3': 1}]
}
添加回答
舉報(bào)