从字典中删除特定位置

时间:2019-06-20 17:06:52

标签: python dictionary

我需要从潜在玩家的字典中删除Pitchers('P')。 我尝试设置

team: [player for player in players_dict.keys() if player.team == team and player.positions != 'P']

但是它仍然会创建一个包含玩家的字典。

class TeamStacksRule(OptimizerRule):
    def apply(self, solver, players_dict):
        stacks = self.optimizer.team_stacks
        players_by_teams = {
            team: [player for player in players_dict.keys() if player.team == team]
            for team in self.optimizer.available_teams
        }

        stacks_dict = {}
        for i, stack in enumerate(sorted(stacks, reverse=True), start=1):
            stacks_dict[stack] = i

        for stack, total in stacks_dict.items():
            combinations_variables = []

            for team, players in players_by_teams.items():
                solver_variable = solver.add_variable('teams_stack_%d_%s' % (stack, team))
                combinations_variables.append(solver_variable)
                variables = [players_dict[player] for player in players]
                solver.add_constraint(variables, None, SolverSign.GTE,
                                      stack * solver_variable)
            solver.add_constraint(combinations_variables, None, SolverSign.GTE, total)

0 个答案:

没有答案