使用两个字典中的元素。对工作 post 和候选人进行排名,并将最佳候选人分配给最佳 post

Working with elements on two dictionaries. Ranking job posting and candidates and assigning best candidate to best post

我的程序中有两个实例:代理商和公司。 我想要一个新的 class 来工作,这样我就可以控制代理人到公司的分配。

我创建的class有两个词典。我将职位和候选人添加到每本词典中。每个字典都以公司或代理 ID 为关键字。我让它在我程序的其他模块中工作。

我将方法调用到 assign_post 以将候选人与职位匹配。但是好像不行。

整个class是:

class Posting(object):

    def __init__(self):
        self.available_postings = {}
        self.candidates = {}

    def add_post(self, firm):
        self.available_postings[firm.get_firm_id()] = firm

    def add_candidate(self, agent):
        self.candidates[agent.get_id()] = agent

    def assign_post(self):
        # Rank positions by wage and rank employees by qualifications
        # Make a match

        while len(self.candidates) > 0 and len(self.available_postings) > 0:
            # Best qualification
            dummy_best = self.candidates.popitem()
            for key in self.candidates.keys():
                if dummy_best.get_qual() > self.candidates[key].get_qual():
                    dummy_best = self.candidates[key]
            # Higher wage
            dummy_higher = self.available_postings.popitem()
            for key in self.available_postings.keys():
                if dummy_higher.set_wage_base() > self.available_postings[key].set_wage_base():
                    dummy_higher = self.available_postings[key]
            # Assignment. Firm has method add_employee
            dummy_higher.add_employee(dummy_best)
            # Remove agent and firm from list
            del self.available_postings[dummy_higher.get_firm_id()]
            del self.candidates[dummy_best.get_id()]

您的代码中的错误是您丢失了从 popitem() 收到的项目,如果它不是 best/higher。尝试接收元素而不从 dict 中删除它,进行赋值,然后删除它。