Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 已使用CSV文件对男性和女性进行排序_Python_Simulated Annealing - Fatal编程技术网

Python 已使用CSV文件对男性和女性进行排序

Python 已使用CSV文件对男性和女性进行排序,python,simulated-annealing,Python,Simulated Annealing,我需要在团队中划分人员,每个团队都有一定数量的男性和女性,我已经编写了以下代码,但我不知道如何修改它,以便生成的团队中有适当数量的男性和女性 代码是: 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

我需要在团队中划分人员,每个团队都有一定数量的男性和女性,我已经编写了以下代码,但我不知道如何修改它,以便生成的团队中有适当数量的男性和女性

代码是:

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)

如果可以接受,您可以通过以下操作使每个团队的男性和女性人数相同(例如,每个团队有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])

现在团队列表将容纳您的团队。Kepp记住,你必须以不同的方式随机分配剩余人员。希望这有帮助

那么,如果你真的能创建9个单独的团队列表来生成9个团队,每个团队都有我需要的数量?我已经读了你的评论好几次了,但对我来说仍然没有意义。。。很抱歉问题是什么?