简体   繁体   English

如何在python中将具有字典列表作为值的字典对象排序?

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

I am trying to sort below dictionary based on "resume_match_score" in descending order. 我正在尝试根据降序对基于“ 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}]

I tried as below and this seems to be working but giving only key instead of full dictionary object. 我尝试如下,这似乎正在工作,但只提供密钥而不是完整的字典对象。

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

and

result = ['4722', '16334']

How to get complete dictionary in sorted order based on key resume_match_score ? 如何基于键resume_match_score以排序顺序获取完整的字典?

Thanks in advance. 提前致谢。

I am not sure if this is the proper way to do it but you can go through a for loop and change every key with a tuple of (key, value) pair. 我不确定这是否是正确的方法,但是您可以通过for循环并使用(键,值)对的元组更改每个键。

Basically you can just add by something like this: 基本上,您可以添加以下内容:

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