I need some assistance in these functions. The function is that I have a list of employees and they are divided into 3 sections: manager, assist and emp.
Below is approach I created to randomly select n number of employees in list
list_sample=["Manager 1","Manager 2","Manager 3","Manager 4","Assist 1","Assist 2","Assist 3","Assist 4","Emp 1","Emp 2","Emp 3","Emp 4","Emp 5","Emp 6"]
random.shuffle(list_sample)
total_num=4
temp_re_list=list_sample[:total_num]
The function I want is to randomly select employees from the list but have the ability to choose based on certain conditions if the list allows it. For example if I need 6 employees and from these 6 i need at least 2 managers along with 1 assist. If the list doesn't contain manager or assist then it will try to provide any emp as long as it gives me the same total emp. Just to be clear the list_sample will allows be greater or equal to total_num
list_sample=["Manager 1","Manager 2","Manager 3","Manager 4","Assist 1","Assist 2","Assist 3","Assist 4","Emp 1","Emp 2","Emp 3","Emp 4","Emp 5","Emp 6"]
total_num=6
manager_num=1
assist_num=2
emp_num=3
# another example of the list
list_sample=["Assist 1","Assist 2","Assist 4","Emp 2","Emp 3","Emp 5","Emp 6"]
manager_num=1
assist_num=2
Currently, I'm think of dividing list_sample into multiple list but i don't think its an efficient way
import random
list_sample=["Assist 1","Assist 2","Assist 3","Assist 4","Emp 1","Emp 2","Emp 3","Emp 4","Emp 5","Emp 6"]
total_num=6
manager_num=1
assist_num=2
emp_num=3
res = []
manager_list=[]
assist_list=[]
emp_lists=[]
random.shuffle(list_sample)
for x in list_sample:
if "Manager" in x:
manager_list.append(x)
elif "Assist" in x:
assist_list.append(x)
elif "Emp" in x:
emp_lists.append(x)
If anyone can guide me into how i can develop this function, it will be great
Thank you
4条答案
按热度按时间azpvetkf1#
Simply use simple random and if conditions like so:
eval()
*ru9i0ody2#
I think that split to separate lists isn't necessarily a bad idea. It will provide you with flexibility.
For example you can do something like this
Anyway, I think that not dividing to groups or sorting the array will require more loop iterations on the whole list which is not efficient.
wkftcu5l3#
This solution allows you to choose the type of employees for a role.
For instance, to select managers first by manager, then assistant, then Employee:
Code
Usage
Using default manager, assistant, and employee categories
Example 2
Allows selection of managers from Managers or Employees by changing default manager_categories
x759pob24#
Your code is efficient. Here is an another way of doing the same.
Output :