python - 有没有办法从个人列表中找到最佳个人,然后将其附加到列表中?
问题描述
我正在设置一个列表,其中我有几个具有编程技能和他们的进度的人。我目前处于困境中,因为我正试图根据他们每美元所涵盖的技能从该列表中大致找到最佳人选。我应该使用什么算法来单独评估个人?
我一直试图让 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
解决方案
不错的优雅答案@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
推荐阅读
- php - Wordpress cronjob 每 5 秒不工作
- snakemake - 安装旧版本的snakemake
- json.net - 是否可以将 Newtonsoft.Json 配置为忽略具有 [ScriptIgnore] 属性的属性
- instruction-set - 我不明白的 Risc-v 指令
- python-3.x - 你如何以python方式调用子类?
- node.js - 在浏览器中转换 javascript+jsx 源代码的最佳约定?
- spring - Spring REST ResponseEntity当我们有两个对象时如何返回响应
- matlab - 如何在MATLAB中逐字节读取二进制格式
- css - 使用浏览器窗口的 100% 高度/宽度的多列 HTML 网格布局
- android - POST 请求 android 应用程序使用 volley 到具有 API(.net 核心)的本地 Microsft 服务器