dimanche 27 février 2022

python dictionary is overwriting itself in a for loop which should not be possible

I am having trouble understanding why the python function does not output a dictionary with the appended shuffled lists but instead creates a dictionary of only the last list created. I have rewritten the function many times but continue to run into the same problem.

#simple schedule fitness test run
#imports
from itertools import permutations
import random
import numpy as np

#create teachers with classes and students with classes
brian_classes = ['akstuddies', 'sci_ms', 'sci_ms', 'physics', 'comp_sci', 'conf']
becky_classes = ['akstuddies', 'lit_ms', 'lit_ms', 'lit', 'conf', 'debate']
teachers = [brian_classes, becky_classes]
#creates inital population of n schedules by shuffling the schedule of each teacher n times
def initial_population(teachers_list, population_size):
    schudule_list_population = {}
    for i in range(0, population_size):
        shuffled_teachers = []
        schudule_list_population[i] = [teacher for teacher in teachers_list]
        for teacher in schudule_list_population[i]:
            np.random.shuffle(teacher)
            shuffled_teachers.append(teacher)
            #print(teacher)
        schudule_list_population[i] = shuffled_teachers
        print(schudule_list_population)
    return schudule_list_population

print(initial_population(teachers, 2))

output:

{0: [['akstuddies', 'conf', 'physics', 'sci_ms', 'sci_ms', 'comp_sci'], ['lit_ms', 'lit', 'conf', 'debate', 'lit_ms', 'akstuddies']]}

{0: [['sci_ms', 'physics', 'comp_sci', 'akstuddies', 'sci_ms', 'conf'], ['conf', 'lit_ms', 'lit_ms', 'akstuddies', 'debate', 'lit']], 1: [['sci_ms', 'physics', 'comp_sci', 'akstuddies', 'sci_ms', 'conf'], ['conf', 'lit_ms', 'lit_ms', 'akstuddies', 'debate', 'lit']]}

{0: [['sci_ms', 'physics', 'comp_sci', 'akstuddies', 'sci_ms', 'conf'], ['conf', 'lit_ms', 'lit_ms', 'akstuddies', 'debate', 'lit']], 1: [['sci_ms', 'physics', 'comp_sci', 'akstuddies', 'sci_ms', 'conf'], ['conf', 'lit_ms', 'lit_ms', 'akstuddies', 'debate', 'lit']]}



Aucun commentaire:

Enregistrer un commentaire