Python Forum
Filter list respecting seniority & no more than 3 same number - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Filter list respecting seniority & no more than 3 same number (/thread-4629.html)

Pages: 1 2


Filter list respecting seniority & no more than 3 same number - Azerate - Aug-30-2017

General idea:

Number are 1 to 6
Each Person chooses 5 number.
Seniority is 1 the oldest & 5 the newest
If number repeat after 3 times, select next available from the chosen numbers

Person 1 chose 1,2,3,4,5
Person 2 chose 1,2,3,4,5
Person 3 chose 1,2,3,4,5
Person 4 chose 1,2,3,4,5
Person 5 chose 2,3,4,5,6

So basicly the list would be something like this right ? :

p1 = 1,2,3,4,5
p2 = 1,2,3,4,5
p3 = 1,2,3,4,5
p4 = 1,2,3,4,5
p5 = 2,3,4,5,6

I would like to have the final result filtered as such:

P1 = 1,2
P2 = 1,2
P3 = 1,2
P4 = 3,4
P5 = 3,4

I've read that "itertools" can be used to achieve such complex algorithm, but i need some help, anyone can give me some pointers?


RE: Filter list respecting seniority & no more than 3 same number - ichabod801 - Aug-31-2017

You have a list of lists ([p1, p2, p3, ...]). Loop through that. Then loop through the sublist (sorted), picking the top two values that haven't been picked three times already. You don't need itertools, you just need a couple loops and a way to keep track of the values you've already chosen (a dictionary would work well, or a list since they are all integers).


RE: Filter list respecting seniority & no more than 3 same number - Azerate - Aug-31-2017

Would
from multiprocessing import Process, Queue
be an option (im not quite sure what id actually do, im still testing it as i write those line ) where:

Process = 1,2,3,4,5,6
Queue = Chosen number

and person would be in an
OrderedDict

I want to make a formula that would assign the vacancy of 70 people, giving them 5 week choice, then, by seniority, it would assign them automatically the vacancy they chose, respecting the fact only 3 people can be on vacancy the same week & that the preferences each person selects is ranks from most preferred to least preferred week. then, would result in two weeks assigned to each of them

So far this is what i achieved

from collections import OrderedDict
from multiprocessing import Process, Queue


AvalableWeekPool = Process
SelectedWeek = Queue
od = OrderedDict()

Process = [1,2,3,4,5]
Queue = []

od['Jean'] =
od['Claude'] =
od['Van'] =
od['Dam'] =

print("\nVacancy Week schedule assigned:\n")
for key ,value in od.items():
    print(key ,value)

I believe

from collections import OrderedDict

od = OrderedDict()

WeekPool = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,
           40,41,42,43,44,45,46,47,48,49,50,51,52]

od['Jean'] = [42,12,51,31,22]
od['Claude'] = [11,11,49,23,51]
od['Van'] = [32,15,1,2,5]
od['Dam'] = [1,2,3,4,50]
Would make more sense for now.


RE: Filter list respecting seniority & no more than 3 same number - Azerate - Aug-31-2017

from collections import OrderedDict

WeekPool = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,
           40,41,42,43,44,45,46,47,48,49,50,51,52]

Employee = OrderedDict([('Jean',1,2,3,4,5),('Claude',1,2,3,4,5),('Van',1,2,3,4,5),('Dam',1,2,3,4,5)])
I cannot edit or remove post, only have right to edit posts up to 10 minutes so ...

from collections import OrderedDict

WeekPool = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,
           40,41,42,43,44,45,46,47,48,49,50,51,52]

Employee = OrderedDict([('Jean',1),('Jean',2),('Jean',3),('Claude',1),('Van',1),('Dam',1)])
How can i make it so the after 'Jean',1 is taken from the WeekPool ?


RE: Filter list respecting seniority & no more than 3 same number - ichabod801 - Aug-31-2017

You don't need multiprocessing. You just need two loops, and a way to track what's been chosen.

weeks = [1, 2, 3, 4, 5]
tracking = {week_num: 0 for week_num in weeks}
assignments = [ ]
for preferences in [[1, 2, 3, 4], [2, 3, 4, 5], [1, 2, 3, 4], [2, 3, 4, 5]]:
    assignments.append([ ])
    for week_num in preferences:
        if tracking[week_num] < 3:
            assignments[-1].append(week_num)
            tracking[week_num] += 1
            if len(assignments[-1]) == 2:
                break



RE: Filter list respecting seniority & no more than 3 same number - Azerate - Aug-31-2017

Wow, i would have never been able to accomplish such a thing, you are a genius, i will play around with it to make sure i understand all the aspect of it.

Much respect man.

So the "assignments ="

Would be the OrderedList of the employee?

I am reading that it can only append if it's a list and not an OrderedDict.

So i would have to change the Employee OrderedDict to a list?


RE: Filter list respecting seniority & no more than 3 same number - ichabod801 - Aug-31-2017

I think you would have two dictionaries: one with the employee names as keys and a list of their preferred weeks as the value, and one with the employee names as keys and a list of their assigned weeks as the value. In my code, I'm just using the two lists, I don't have them in dictionaries. I think only the first one needs to be an OrderedDict, since you need to keep the employees in seniority order. The other one can be a plain dict, since you just need to know what weeks the person is assigned to.

Once you have this up and working, you should look at classes (there's a tutorial like in my signature). Right now there's one list with the employee's preferences, one with their assignments, and maybe one keeping track of their seniority. With an Employee class, you could make one list that stores all that information, and knows how to sort itself by seniority.


RE: Filter list respecting seniority & no more than 3 same number - nilamo - Aug-31-2017

(Aug-31-2017, 04:21 PM)Azerate Wrote: Would
from multiprocessing import Process, Queue
be an option (im not quite sure what id actually do, im still testing it as i write those line )

Sure, that's an option. It'd be a bad option, but it's definitely an option.

multiprocessing.Process will spawn a new process, so you can do two things at the same time, so you can use more than one core of your processor (or more than one processor, if you have them). That's almost never helpful before you have something that works, since it's used to make something that works faster.

multiprocessing.Queue is a way to queue data that can be passed between processes. For this instance, you gain almost nothing from using it.


RE: Filter list respecting seniority & no more than 3 same number - nilamo - Aug-31-2017

(Aug-31-2017, 07:17 PM)Azerate Wrote: So the "assignments ="

Would be the OrderedList of the employee?
 

No, that's a list: [ ]. A quirk of the forum's editor removes empty lists when you preview posts. I went back and edited that post so it should be runnable code now.


RE: Filter list respecting seniority & no more than 3 same number - Azerate - Aug-31-2017

Thank for those clarifications, i was not sure of the reference in the code you wrote, i will definitely go read and practice this "Classes" tutorial on your signature.

For now, i will spend the next two hours working on the code, if you don't mind, I'll post what i have accomplished with what you have explained once completed to see if I've done it correctly.

You are of great help, i upvoted you :)