[Tutor] Regarding help in python algorithm
Hi, I am a student at an university. Currently I was working on an algorithm using python. It is based on scheduling the teachers to their nearest venues. And at the venues there can be atmost 400 teachers and these are to be divided into the Batches of 40 i.e. ten batches. All the batches will have teachers having same group number assigned to them. and al the batches should get only the two days from the working days in the month. This is only the last part of the complete algorithm. I am sending the files associated to it and the code that I have made till now. Please help me in making it as I need it urgently. Thanks in advance. Regards Suhit import math import csv import pdb import pandas as pd import numpy as np from math import radians, sin, cos, acos def distanceCalculator(latitude1,longitude1,latitude2,longitude2): slat = radians(latitude1) slon = radians(longitude1) elat = radians(latitude2) elon = radians(longitude2) dist = 6371.01 * acos(sin(slat)*sin(elat) + cos(slat)*cos(elat)*cos(slon - elon)) return dist #df = pd.read_csv("venueData.csv",header=None) df2 = pd.read_csv("mtdata.csv",header=None) df2.columns = ['name','location','latitude','longitude','subject'] df = pd.read_csv("venueData.csv",header=None) df.columns=['name','latitude','longitude'] #df2 = pd.read_csv("mtdata.csv",header=None) #df2.columns = ['name','location','latitude','longitude','subject'] teacher = pd.read_csv("teachers.csv") teacher['Latitude'] = teacher['Latitude'].apply(lambda x: x.rstrip(",") if type(x) == str else x ) teacher['Longitude'] = teacher['Longitude'].apply(lambda x: x.rstrip(",") if type(x) == str else x ) listEmpty = [] dictionaryTeacher = {} for i,ex in teacher.iterrows(): lat1 = ex['Latitude'] lon1 = ex['Longitude'] Id = i for b,c in df.iterrows(): lat2 = c['latitude'] lon2 = c['longitude'] nameVen = c['name'] listEmpty.append((distanceCalculator(float(lat1),float(lon1),float(lat2),float(lon2)),Id,b)) demian = [] listEmpty.sort() demian = listEmpty[0] dictionaryTeacher[ex['Name']] = demian listEmpty = [] demian = [] DataTeacher = pd.DataFrame(columns=['Teacher','Distance','Venue','Eng','Hindi','Maths']) number = 3 for ex in dictionaryTeacher: DataTeacher= DataTeacher.append({'Teacher':ex,'Distance':dictionaryTeacher[ex][0],'Venue':df.loc[dictionaryTeacher[ex][2]]['name'],'Eng':teacher.loc[dictionaryTeacher[ex][1]]['Eng'],'Hindi':teacher.loc[dictionaryTeacher[ex][1]]['Hindi'],'Maths':teacher.loc[dictionaryTeacher[ex][1]]['Maths']},ignore_index=True) days = pd.read_csv("days.csv") days.columns = ['January', 'January:Days', 'February', 'Feburary:Days', 'March', 'March:Days', 'April', 'April:Days', 'May', 'May:Days', 'June', 'June:Days', 'July', 'July:Days', 'August', 'August:Days', 'September', 'September:Days', 'October', 'October:Days', 'November', 'November:Days', 'December', 'December:Days'] df.columns=['name','latitude','longitude'] df['name'] = df['name'].apply(lambda x: x.rstrip()) df2['name'] =df2['name'].apply(lambda x: x.rstrip()) venue = {} for i,k in enumerate(df['name']): if (k not in venue): venue[k] = {'January': 0 ,'February':0 , 'March': 0 , 'April':0, 'May':0,'June ':0 ,'July':0 , 'August':0,'September':0,'October':0,'November':0,'December':0} teacher = {} for i,k in enumerate(df2['name']): if (k not in teacher): teacher[k] = {'January': 0 ,'February':0 , 'March': 0 , 'April':0, 'May':0,'June ':0 ,'July':0 , 'August':0,'September':0,'October':0,'November':0,'December':0} dictionary = {} liste = [] for i,ex in df2.iterrows(): nameT = ex['name'] lat1 = ex['latitude'] lon1 = ex['longitude'] sub = ex['subject'] Id = i for b , x in df.iterrows(): nameM = x['name'] lat2 = x['latitude'] lon2 = x['longitude'] id2 = b liste.append((distanceCalculator(lat1,lon1,lat2,lon2),i,id2)) liste.sort() dictionary[ex['name']] = liste[0:3] liste = [] Data = pd.DataFrame(columns=['Trainer','Venue','Distance','Subjects','Location']) for ex in dictionary: for i , k in enumerate(dictionary[ex]): Data = Data.append({'Trainer':ex ,'Venue': df.loc[dictionary[ex][i][2]]['name'],'Distance': dictionary[ex][i][0],'Subjects':df2.loc[dictionary[ex][i][1]]['subject'],'Location':df2.loc[dictionary[ex][i][1]]['location']},ignore_index=True) Data['Month'] = -1 for i,ex in Data.iterrows(): Train = ex['Trainer'] Venue = ex['Venue'] for ex in teacher[Train]: if(teacher[Train][ex]==
Re: [Tutor] enumerate over Dictionaries
Hi, I have made the complete program but when I am compiling the program it is showing errors. Can you please help to resolve this? The code is in the file attached with this mail. On Fri, Jul 5, 2019 at 10:44 PM Animesh Bhadra wrote: > Thanks Alan and Mats for the explanation. > > On 05/07/19 19:57, Mats Wichmann wrote: > > On 7/4/19 3:53 PM, Alan Gauld via Tutor wrote: > > > >>> Does this means that the Dict is ordered? or it is implementation > dependent? > >> Neither, it means the items in a list always have indexes > >> starting at zero. > >> > >> By pure coincidence dictionaries in recent Python versions (since 3.6 > >> or 3.7???) retain their insertion order. But that was not always the > >> case, but the result would have been the same so far as the 0,1,2 bit > goes. > >> > > To be a little more precise, in 3.6 CPython insertion order was > > preserved as an artefact of the new implementation of dicts, but not > > promised to be that way. Since 3.7 it is guaranteed (it is actually in > > the language specification, so other Pythons have to do this too now). > > > > It's still not the same as a collections.OrderedDict, which has some > > useful additional features in case you care a lot about ordering. > > > > ___ > > Tutor maillist - Tutor@python.org > > To unsubscribe or change subscription options: > > https://mail.python.org/mailman/listinfo/tutor > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > import math import csv import pdb import pandas as pd import numpy as np from math import radians, sin, cos, acos def distanceCalculator(latitude1,longitude1,latitude2,longitude2): slat = radians(latitude1) slon = radians(longitude1) elat = radians(latitude2) elon = radians(longitude2) dist = 6371.01 * acos(sin(slat)*sin(elat) + cos(slat)*cos(elat)*cos(slon - elon)) return dist df = pd.read_csv("venueData.csv",header=None) df.columns=['name','latitude','longitude','district','block'] df2 = pd.read_csv("mtdata.csv",header=None) df2.columns = ['name','location','latitude','longitude','subject'] teacher = pd.read_csv("teachers.csv") teacher.head() df.head() teacher['Latitude'] = teacher['Latitude'].apply(lambda x: x.rstrip(",") if type(x) == str else x ) teacher['Longitude'] = teacher['Longitude'].apply(lambda x: x.rstrip(",") if type(x) == str else x ) listEmpty = [] dictionaryTeacher = {} for i,ex in teacher.iterrows(): lat1 = ex['Latitude'] lon1 = ex['Longitude'] Id = i for b,c in df.iterrows(): lat2 = c['latitude'] lon2 = c['longitude'] nameVen = c['name'] listEmpty.append((distanceCalculator(float(lat1),float(lon1),float(lat2),float(lon2)),Id,b)) demian = [] listEmpty.sort() demian = listEmpty[0] dictionaryTeacher[ex['Name']] = demian listEmpty = [] demian = [] DataTeacher = pd.DataFrame(columns=['Teacher','Distance','Venue','Eng','Hindi','Maths','TeacherId']) number = 3 for ex in dictionaryTeacher: DataTeacher= DataTeacher.append({'Teacher':ex,'Distance':dictionaryTeacher[ex][0],'Venue':df.loc[dictionaryTeacher[ex][2]]['name'],'Eng':teacher.loc[dictionaryTeacher[ex][1]]['Eng'],'Hindi':teacher.loc[dictionaryTeacher[ex][1]]['Hindi'],'Maths':teacher.loc[dictionaryTeacher[ex][1]]['Maths'],'TeacherId':dictionaryTeacher[ex][1]},ignore_index=True) days = pd.read_csv("days.csv") days.columns = ['January', 'January:Days', 'February', 'Feburary:Days', 'March', 'March:Days', 'April', 'April:Days', 'May', 'May:Days', 'June', 'June:Days', 'July', 'July:Days', 'August', 'August:Days', 'September', 'September:Days', 'October', 'October:Days', 'November', 'November:Days', 'December', 'December:Days'] df.columns=['name','latitude','longitude','district','block'] df['name'] = df['name'].apply(lambda x: x.rstrip()) df2['name'] =df2['name'].apply(lambda x: x.rstrip()) venue = {} for i,k in enumerate(df['name']): if (k not in venue): venue[k] = {'January': 0 ,'February':0 , 'March': 0 , 'April':0, 'May':0,'June ':0 ,'July':0 , 'August':0,'September':0,'October':0,'November':0,'December':0} teacher = {} for i,k in enumerate(df2['name']): if (k not in teacher): teacher[k] = {'January': 0 ,'February':0 , 'March': 0 , 'April':0, 'May':0,'June ':0 ,'July':0 , 'August':0,'September':0,'October':0,'November':0,'December':0} teacher['Ajmal'] dictionary = {} liste = [] for i,ex in df2.iterrows(): nameT = ex['name'] lat1 = ex['latitude'] lon1 = ex['longitude'] sub = ex['subject'] Id = i