You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何高效实现字典列表与需求技能列表匹配并递增rank值?

优化技能匹配计算Rank的高效实现

嘿,我来帮你优化这段技能匹配的代码!你当前的实现用了三重循环,数据量一大效率就容易拉胯——毕竟三重嵌套的时间复杂度是O(NMK),咱们可以利用集合的快速查找特性来简化过程,把效率提上去,同时代码也更清爽。

核心优化思路

把需求技能列表转成集合,这样判断某个技能是否属于需求的操作是O(1)的;另外,我们可以直接计算员工技能和需求技能的交集大小,一次性得到匹配的数量,不用逐个循环比对每个技能。

优化后的代码

requirements = ["JSF","JSP","Spring"]
req_set = set(requirements)  # 转成集合,查找速度直接起飞

Employee_records = [
    {'job_seeker_id': 1, 'skills': ['Spring', 'JSP', 'JSF'], 'experience': '5 years', 'location': 'stockholm', 'rank': 3},
    {'job_seeker_id': 2, 'skills': ['Servlets', 'JSP'], 'experience': '2 years', 'location': 'gothenburg', 'rank': 1},
    {'job_seeker_id': 3, 'skills': ['JSP'], 'experience': '4 years', 'location': 'lund', 'rank': 1},
    {'job_seeker_id': 4, 'skills': ['Servlets'], 'experience': '3 years', 'location': 'malmo', 'rank': 0},
    {'job_seeker_id': 5, 'skills': ['Hibernate', 'Servlets'], 'experience': '1 years', 'location': 'stockholm', 'rank': 0}
]

for emp in Employee_records:
    # 直接算交集长度,得到匹配的技能数
    match_count = len(set(emp['skills']) & req_set)
    emp['rank'] += match_count

针对小技能列表的细节优化

如果员工的技能列表普遍很短,转集合的那点开销也可以省掉,改成逐个检查的方式,内存占用更低,效率也不差:

for emp in Employee_records:
    match_count = 0
    for skill in emp['skills']:
        if skill in req_set:
            match_count += 1
    emp['rank'] += match_count

为啥这俩方案更高效?

  • 集合的in操作是O(1),而列表的in是O(K)(K是需求技能数),每个技能的检查速度快了不止一个量级
  • 砍掉了原来的第三重循环,嵌套层级从3层降到2层,逻辑更直观,循环次数也大幅减少

不管是处理小规模数据还是大规模员工列表,这俩方案都比你原来的三重循环更靠谱,代码可读性也提升了不少~

内容的提问来源于stack exchange,提问作者yeshwanth

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 22:57:56