首页 > 解决方案 > 有没有办法从个人列表中找到最佳个人,然后将其附加到列表中?

问题描述

我正在设置一个列表,其中我有几个具有编程技能和他们的进度的人。我目前处于困境中,因为我正试图根据他们每美元所涵盖的技能从该列表中大致找到最佳人选。我应该使用什么算法来单独评估个人?

我一直试图让 for 循环迭代,但它不会在位置上移动。它仅在查看第一个单元格后结束。

jess = (["php", "java"], 200)

clark = (["php", "c++", "go"], 1000)

john = (["lua"], 500)

cindy = (["php", "go", "word"], 240)

candidates = [jess, clark, john, cindy]

project = ["php", "java", "c++", "lua", "go"]

def team_of_best_individuals(project, candidates):       

##note that in the list candidates, I tried to divide the number of items in the tuple by the cost, in order to find the skill per dollar

   def team_of_best_individuals(project, candidates):
   skillList = []
   for name in (candidates):
       len(name)
       for skill in name[0]:
            skillList.append(skill)

            if len(skillList) == len(name):
                num_of_skills=len(skillList)
                cost = name[1]
                num_skill_per_dollar = num_of_skills/cost
                return num_skill_per_dollar, candidates[0:4]
print("skill per dollar="+str(team_of_best_individuals(project, candidates)))

预期(产出)必须是每美元涵盖最多技能的人。返回值必须是列表中人员的位置整数。

例如

0 - 4

标签: pythonpython-3.xalgorithmgreedy

解决方案


不错的优雅答案@Mark Meyer,我想在这里补充一点,我认为您的问题中缺少这一点。它需要寻找作为项目一部分的技能。如果候选人所拥有的任何技能不属于其中的一部分,则必须对其进行打折。我试着写一些可以做到这一点的东西。正如他指出的那样,单独使用候选名称而不是变量名称可能是值得的。

jess = (["php", "java"], 200)

clark = (["php", "c++", "go"], 1000)

john = (["lua"], 500)

cindy = (["php", "go", "word"], 240)

candidates = [jess, clark, john, cindy]

project = ["php", "java", "c++", "lua", "go"]

def team_of_best_individuals(project, candidates):
    best = ('A', 0)
    for ind, candidate in enumerate(candidates):
        skills = candidate[0]
        skillperdollar = len([skill for skill in skills if skill in project])/candidate[1]
        if skillperdollar > best[1]:
            best = (ind, skillperdollar)
    return best

推荐阅读