创建调度程序

时间:2019-07-11 06:33:41

标签: python

我正在尝试创建要执行的操作的时间表,但是这些操作只能由某些对象执行。我真的在努力寻找一种有效的算法,我写的所有东西看起来都不是“整洁”的,并且涉及到我很满意的多维数组,但是我不禁觉得有一种更好的方法可以做到这一点。 (感谢您的帮助)

例如,假设我创建了一个能够完成操作的人员数组,但假设一个名为“ Sally”的人员只能完成某些操作,而“ Kevin”则只能完成一组不同的操作...我想要完全耗尽操作数组,而不对有能力执行操作的任何人重复测试。

我创建了一个人数组,我创建了一个数组操作。当某些操作与某些无法执行该操作的人配对时,我已经做出了标记。

这很快就变成了一个热门话题,虽然我可以得到一些“可行”的东西,但我知道必须有一种更好的方法来做到这一点,我希望这种方法可以“面向未来”操作列表增加或新来的人能够执行更多或更少的操作。

1 个答案:

答案 0 :(得分:0)

我也有类似的情况。您可以执行以下操作:

>>> people_map = { "Kevin" : ["operation1", "operation3"], "Sally" : ["operation1", "operation4"], "Aaron" : [] }

>>> operations = ["operation1", "operation2", "operation3", "operation4", "operation5"]

>>> initial_set = set(operations)

>>> for person in people:
        person_operations = people_map[person]
        for person_operation in person_operations:
            print("%s can perform %s, so removing it" % (person, person_operation))
            initial_set.discard(person_operation)
            #
            # Assign some operation to someone from initial_set
            #  It would have operations that are al-ready not performed 
            #   by anyone else
            #


Kevin can perform operation1, so removing it
Kevin can perform operation3, so removing it
Sally can perform operation1, so removing it
Sally can perform operation4, so removing it
>>> initial_set

{'operation5', 'operation2'}
>>> 

通常,您可以在python中使用sets。您还可以找出集合的并集和交集