簡體   English   中英

如何在python中將具有字典列表作為值的字典對象排序?

[英]How to sort dictionary object having list of dictionary as value in python?

我正在嘗試根據降序對基於“ resume_match_score”的字典進行排序。

{'16334': [{'skill_match': {'java': 33,
    'python': 5,
    'swing': 1,
    'apache cassandra': 1},
   'skill_match_score': 0.8},
  {'doc_attachment': '97817_1560102392mahesh-java.docx',
   'document_path': '06_2019',
   'firstname': 'nan',
   'lastname': 'nan'},
  {'job_title_match': {'java developer': 3}, 'job_title_match_score': 0.5},
  {'resume_match_score': 0.71}],
 '4722': [{'skill_match': {'java': 24, 'python': 1, 'hadoop': 31},
   'skill_match_score': 0.6},
  {'doc_attachment': '4285_1560088607Srujan_Hadoop.docx',
   'document_path': '06_2019',
   'firstname': 'nan',
   'lastname': 'nan'},
  {'job_title_match': {'hadoop developer': 3, 'java developer': 2},
   'job_title_match_score': 1.0},
  {'resume_match_score': 0.72}]

我嘗試如下,這似乎正在工作,但只提供密鑰而不是完整的字典對象。

result = sorted(test_d, key=lambda k: test_d[k][3].get("resume_match_score", 0), reverse=True)

result = ['4722', '16334']

如何基於鍵resume_match_score以排序順序獲取完整的字典?

提前致謝。

我不確定這是否是正確的方法,但是您可以通過for循環並使用(鍵,值)對的元組更改每個鍵。

基本上,您可以添加以下內容:

real_result = []
for key in result:
    real_result.append((key, dict.get(key))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM