2013-05-24 2 views
0

사람들을 팀으로 나눌 필요가 있습니다. 각 팀마다 해당 팀에 필요한 남성과 여성이 필요합니다. 다음 코드를 작성했지만 어떻게 작성해야할지 모르겠습니다. 생성 된 팀이 올바른 양의 남성과 여성을 갖도록 수정하십시오.CSV 파일로 남성과 여성을 이미 정렬합니다.

코드는 다음과 같습니다이 허용 있다면

from random import randint 

def generateTeams(peopleList, numberOfTeams): 
    """ Given a list of of people and a number of teams (int) to split them into, 
     " returns a list of randomly allocated teams. If the people do not divide 
     " evenly into teams then the optimum solution will be chosen. 
    """ 
    #Calculate optimum team size 
    idealTeamSize = len(peopleList)/numberOfTeams 

    #Empty lists 
    teams = [] 
    teamsReturn = [] 

    #Generate empty teams 
    for i in range(0, numberOfTeams): 
     teams.append([]); 

    #While we still have people 
    while(len(peopleList) > 0): 
     randomIndex = randint(0, len(peopleList)-1) 

     #Or until there are no teams left in the initial list 
     if (len(teams) == 0): 
      print len(peopleList) 
      break 

     #Add a random person to the first team in the list 
     teams[0].append(peopleList[randomIndex]) 
     #And remove them from the peopleList 
     peopleList.pop(randomIndex) 

     #If a team has reached optimal size, remove it 
     if (len(teams[0]) >= idealTeamSize): 
      teamsReturn.append(teams.pop(0)) 

    #Randomly allocate remainder 
    while(len(peopleList) > 0): 
     randomIndex = randint(0, len(teamsReturn)-1) 
     teamsReturn[randomIndex].append(peopleList.pop(0)); 

    return teamsReturn 

# Open the file 
f = open('teamselection.csv', 'r') 
# Extract the team names and stats 
TeamNames = f.readline()[:-1].split(',')[1:] 
Men = [int(s) for s in f.readline()[:-1].split(',')[1:]] 
Women = [int(s) for s in f.readline()[:-1].split(',')[1:]] 
MinDriver = [int(s) for s in f.readline()[:-1].split(',')[1:]] 
PrefDriver = [int(s) for s in f.readline()[:-1].split(',')[1:]] 
MinMusic = [int(s) for s in f.readline()[:-1].split(',')[1:]] 
PrefMusic = [int(s) for s in f.readline()[:-1].split(',')[1:]] 
MinMaleLead = [int(s) for s in f.readline()[:-1].split(',')[1:]] 
PrefMaleLead = [int(s) for s in f.readline()[:-1].split(',')[1:]] 
MinFemLead = [int(s) for s in f.readline()[:-1].split(',')[1:]] 
PrefFemLead = [int(s) for s in f.readline()[:-1].split(',')[1:]] 
MinLead = [int(s) for s in f.readline()[:-1].split(',')[1:]] 
PrefLead = [int(s) for s in f.readline()[:-1].split(',')[1:]] 
Importance = [int(s) for s in f.readline()[:-1].split(',')[1:]] 

# Skip the data header line 
f.readline() 
# Read and save the rest of the lines 
Lines = f.readlines() 
f.close() 

# Set up a range T to iterate over 
T = range(len(TeamNames)) 
# Store record for the volunteers. Each is in a list with the following information 
# 0: Name 
# 1: Gender M/F 
# 2: Music score 
# 3: Leadership score 
# 4: Driver (1 is a driver) 
# 5: List of team indicators - 1 is a mandatory team, -1 is an excluded team 

People = [] 
for line in Lines: 
    fields = line[:-1].split(',') 
    People.append([fields[0], fields[1], int(fields[2]), int(fields[3]), int (fields[4]), 
       [int(fields[5+t]) for t in T]]) 
P = range(len(People)) 

#Get actual names into this list 
peopleNames = [] 

#Grab the names 
for i in People: 
    peopleNames.append(i[0] + " " + i[1]) 

#Find teams using names 
print generateTeams(peopleNames, 9) 

답변

0

, 당신은 각 팀은 남성과 여성의 같은 번호를 가지고 만들 수있는 이런 일을 수행하여 (각 팀은 10 명의 남성과 7 명의 여성을 가지고 예) :

male_team_list = generateTeams(list_of_males, num_teams) 
female_team_list = generateTeams(list_of_females, num_teams) 
team_list = [] 
for i in range(num_teams): 
    team_list.append(male_team_list[i] + female_team_list[i]) 

이제 team_list가 팀을 보유하게됩니다. Kepp는 남은 사람들을 무작위로 다르게 할당해야한다는 것을 명심하십시오. 희망이 도움이

+0

그래서 9 개의 팀 목록을 실제로 만들 수있는 필요한 각 필요한 9 개의 팀을 생성하려면? – user2412772

+0

나는 당신의 코멘트를 몇 번 읽었으며 여전히 나에게 의미가 없다 ... 미안하다. 질문이 뭐야? – ahuff44

관련 문제